当前位置:网站首页>ES6新特性class类
ES6新特性class类
2022-06-10 09:18:00 【InfoQ】
类是什么
如何定义类
//类声明 class Person {}; //表达式类 const Animal = class {};类与构造函数区别
class Person {};
function Person1(){};
let p = Person();//Uncaught TypeError: Class constructor Person cannot be invoked without 'new'at
let p1 = Person1();
迭代器和生成器方法
class Person {
//在原型上定义生成器
*nameIterator (){
yield 'jackson';
yield 'bear';
}
//在类上定义生成器
static *colorIterator(){
yield 'blue';
yield 'green';
}
};
//类
let p = new Person();
let names = p.nameIterator();
console.log(names.next().value);//jackson
console.log(names.next().value);//bear
//原型
let colors = Person.colorIterator();
console.log(colors.next().value);//blue
console.log(colors.next().value);//green
类的继承
1. 继承基础
class Exten {};
//继承类
class Bus extends Exten {};
let a = new Bus();
console.log(a instanceof Bus); //true
console.log(a instanceof Exten); //true
function Person(){};
//继承普通构造函数
class Eng extends Person {};
let b = new Eng();
console.log(b instanceof Eng); //true
console.log(b instanceof Person); //true
2.构造函数、HomeObject、Super
class Vehicle {
constructor() {
this.hasEngine = true;
}
}
class Bus extends Vehicle {
constructor() {
// 不要在调用 super()之前引用 this,否则会抛出 ReferenceError
super(); // 相当于 super.constructor()
console.log(this instanceof Vehicle); // true
console.log(this); // Bus { hasEngine: true }
}
}
new Bus();
- super 只能在派生类构造函数和静态方法中使用。
- 不能单独引用 super 关键字,要么用它调用构造函数,要么用它引用静态方法。
- 调用 super()会调用父类构造函数,并将返回的实例赋值给 this。
- super()的行为如同调用构造函数,如果需要给父类构造函数传参,则需要手动传入。
- 如果没有定义类构造函数,在实例化派生类时会调用 super(),而且会传入所有传给派生类的参数。
- 在类构造函数中,不能在调用 super()之前引用 this。
- 如果在派生类中显式定义了构造函数,则要么必须在其中调用 super(),要么必须在其中返回一个对象。
3.继承内置类型
class SuperArray extends Array {}
let a1 = new SuperArray(1, 2, 3, 4, 5);
let a2 = a1.filter(x => !!(x % 2))
console.log(a1); // [1, 2, 3, 4, 5]
console.log(a2); // [1, 3, 5]
console.log(a1 instanceof SuperArray); // true
console.log(a2 instanceof SuperArray); // true
边栏推荐
猜你喜欢

Formula Derivation

ifstream seekg( ) read( )文本操作

QQ微信实现连续发送消息【代码实现】

Online | 100000 bonus! Greaterwms/dvadmin plug-in developer cash incentive activities

【JUC系列】线程池基础使用

Thinking about function declaration

Linear Regression

Configure vscode+cmake under win11

在 Kubernetes 中基于 StatefulSet 部署 MySQL(下)

AWS IOT reference example of Lexin launching esp32-c3
随机推荐
Reference counting and smart pointer for VTK learning
vtk学习之坐标系统
【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具
LeetCode琅琊榜第十九层-有效的括号
printk学习之(一):基本原理
以行为单位 页面的所有的内容都是以行分切的
win11安装Pandoc
Atom, the top stream editor, will leave the historical stage on December 15
How far is your team from continuous deployment in 2022?
The digital collection platform also said that it was "running away": its security was in doubt, and strict supervision was on the way
在 Kubernetes 中基于 StatefulSet 部署 MySQL(上)
36氪首发 | 新一代iPOCT产品持续发展,「伊鸿健康 」完成新一轮数千万元融资
Method of adding status bar in MFC window
What are the advantages of SaaS services
MainActivity
vtk学习之引用计数与智能指针
Linear Regression
vtk学习之RenderCylinder-Lights灯光渲染
关于函数声明的思考
HP notebook - how to wake up a laptop after sleep