ES6语法(11)
来源:互联网 发布:js then方法 编辑:程序博客网 时间:2024/05/29 06:54
ES6中的类
一、定义类的基本方法
{
class Parent{
}
}
此时定义了一个空的类,没有具体内容。
constructor是构造函数,实例化还是用new。
{
class Parent{
constructor(name="jack"){
this.name = name;
}
}
let newParent = new Parent("pull");
console.log(newParent);
}
二、继承 extends
{class Parent{
constructor(name="jack"){
this.name = name;
}
}
class Child extends Parent{
}
console.log(new Child); //继承了Parent中的name
}
继承传递参数
{
class Parent{
constructor(name="parent"){
this.name = name;
}
}
class Child extends Parent{
constructor(name="child"){
super(name); //用自己的参数
this.type="child"; //this必须写在super后面
}
}
console.log(new Child);
}
三、类中的getter setter
{
class Parent{
constructor(name="parent"){
this.name = name;
}
get longName(){
return "longName" + this.name;
}
set longName(value){
this.name = value;
}
}
let newParent = new Parent();
console.log(newParent.longName); //longNameparent
newParent.longName = "jack"; //重新设置了name
console.log(newParent.longName); //longNamejack
}
四、静态方法
从类调用,不是从类的实例调用。
{
class Parent{
constructor(name="parent"){
this.name = name;
}
static tell(){
console.log("tell");
}
}
Parent.tell(); //tell
}
五、静态属性
{
class Parent{
constructor(name="parent"){
this.name = name;
}
}
let newP = new Parent();
newP.prop = 1;
console.log(newP.prop); //1
}
- ES6语法(11)
- ES6语法
- es6语法
- ES6语法篇(其一)
- ES6 语法大概内容
- ES6新语法概览
- ES6核心内容之语法
- ES6 参数默认值语法
- Gulp使用ES6语法
- ES6语法摘要一
- es6语法学习
- ES6语法摘要二
- ES6语法整理
- ES6语法中的let
- ES6语法初探
- es6 — 基本语法
- ES6语法学习总结
- React中的es6语法
- java反射详解之反射作用
- Redis
- 舞步学院为你解答,爱情新手常犯的六个错误
- Java中集合
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
- ES6语法(11)
- Python之向日志输出中添加上下文信息
- 《分布式服务框架原理与实践》读书笔记之第三章 通信框架
- c#经典俄罗斯方块 vs2012开发
- C#中的Invoke()
- ps命令详解
- 冒泡选择排序 二维数组 虚方法 隐藏方法 枚举 类 结构体 构造方法
- KendoUI系列:MultiSelect 多选 智能下拉
- 析构 匿名 密封 多态