es6里class类

/**
* Created by issuser on 2018/11/27.
*///如果静态方法包含this关键字,这个this指的是类,而不是实例。
/*
* (1)类的实例属性

1、类的实例属性可以用等式,写入类的定义之中。
class MyClass {
myProp = 42;
constructor() {
console.log(this.myProp); // 42
}
}
* */
class Foo {
static bar () {
this.baz();
}
static baz () {
console.log('hello');
}
baz () {
console.log('world');
}
}
Foo.bar() // hello
// 上面代码中,静态方法bar调用了this.baz,这里的this指的是Foo类,而不是Foo的实例,等同于调用Foo.baz。另外,
// 从这个例子还可以看出,静态方法可以与非静态方法重名。

//父类的静态方法,可以被子类继承。
class Foo {
static classMethod() {
return 'hello';
}
}
class Bar extends Foo {}
Bar.classMethod() // 'hello'

// 静态方法也是可以从super对象上调用的。
class Foo {
static classMethod() {
return 'hello';
}
}
class Bar extends Foo {
static classMethod() {
return super.classMethod() + ', too';
}
}
Bar.classMethod() // "hello, too"
//目前,只有这种写法可行,因为 ES6 明确规定,
// Class 内部只有静态方法,没有静态属性。


// 静态属性
//类的实例属性可以用等式,写入类的定义之中。
//目前有一个静态属性的提案,对实例属性和静态属性都规定了新的写法。
class MyClass {
myProp = 42;
constructor() {
console.log(this.myProp); // 42
}
}
//上面代码中,myProp就是MyClass的实例属性。在MyClass的实例上,可以读取这个属性。

// 类的静态属性
//类的静态属性只要在上面的实例属性写法前面,加上static关键字就可以了。
class MyClass {
static myStaticProp = 42;
constructor() {
console.log(MyClass.myStaticProp);
}
} // 42
/*
* new.target 属性
new是从构造函数生成实例对象的命令。
ES6 为new命令引入了一个new.target属性,
该属性一般用在构造函数之中,返回new命令作用于的那个构造函数。
如果构造函数不是通过new命令调用的,new.target会返回undefined,
因此这个属性可以用来确定构造函数是怎么调用的。
* */
// 需要注意的是,子类继承父类时,new.target会返回子类。


// super关键字
/*
* super关键字,它在这里表示父类的构造函数,用来新建父类的this对象。
子类必须在constructor方法中调用super方法,否则新建实例时会报错。
这是因为子类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,
加上子类自己的实例属性和方法。如果不调用super方法,子类就得不到this对象。
* */

/*
* ES6 的继承机制完全不同,实质是先将父类实例对象的属性和方法,
* 加到this上面(所以必须先调用super方法),然后再用子类的构造函数修改this。
如果子类没有定义constructor方法,这个方法会被默认添加,代码如下。
也就是说,不管有没有显式定义,任何一个子类都有constructor方法。
class ColorPoint extends Point {
}

// 等同于
class ColorPoint extends Point {
constructor(...args) {
super(...args);
}
* */

/*
* 在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。
* 因为子类实例的构建,基于父类实例,只有super方法才能调用父类实例。

class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
this.color = color; // ReferenceError报错
super(x, y);
this.color = color; // 正确
}
}
* */


/*
* class A {
static hello() {
console.log('hello world');
}
}
class B extends A {}
B.hello() // hello world
上面代码中,hello()是A类的静态方法,B继承A,也继承了A的静态方法。
* */

/*
* 第一种情况,super作为函数调用时,代表父类的构造函数。
* ES6 要求,子类的构造函数必须执行一次super函数。
* class A {}

class B extends A {
constructor() {
super();
}
}
* */

/*
* 注意,super虽然代表了父类A的构造函数,但是返回的是子类B的实例,
* 即super内部的this指的是B,因此super()在这里相当于A.prototype.constructor.call(this)。
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();
}
}
new A() // A
new B() // B
上面代码中,new.target指向当前正在执行的函数。
可以看到,在super()执行时,它指向的是子类B的构造函数,
而不是父类A的构造函数。也就是说,super()内部的this指向的是B。
* */

/*
* 作为函数时,super()只能用在子类的构造函数之中,用在其他地方就会报错。
class A {}
class B extends A {
m() {
super(); // 报错
}
}
* */

/*
* class A {
p() {
return 2;
}
}
class B extends A {
constructor() {
super();
console.log(super.p()); // 2
}
}
let b = new B();
上面代码中,子类B当中的super.p(),就是将super当作一个对象使用。
这时,super在普通方法之中,指向A.prototype,
所以super.p()就相当于A.prototype.p()。
*
* */


/*
* 这里需要注意,由于super指向父类的原型对象,
* 所以定义在父类实例上的方法或属性,是无法通过super调用的。
class A {
constructor() {
this.p = 2;
}
}

class B extends A {
get m() {
return super.p;
}
}
let b = new B();
b.m // undefined
上面代码中,p是父类A实例的属性,super.p就引用不到它。
*
* */


/*
* class A {
constructor() {
this.x = 1;
}
print() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super();
this.x = 2;
}
m() {
super.print();
}
}

let b = new B();
b.m() // 2
上面代码中,super.print()虽然调用的是A.prototype.print(),
但是A.prototype.print()内部的this指向子类B的实例,导致输出的是2,而不是1。
也就是说,实际上执行的是super.print.call(this)。
*
* */


/*
* class A {
constructor() {
this.x = 1;
}
}
class B extends A {
constructor() {
super();
this.x = 2;
super.x = 3;
console.log(super.x); // undefined
console.log(this.x); // 3
}
}
let b = new B();
上面代码中,super.x赋值为3,这时等同于对this.x赋值为3。
而当读取super.x的时候,读的是A.prototype.x,
所以返回undefined。
*
* */


/*
* class Parent {
static myMethod(msg) {
console.log('static', msg);
}
myMethod(msg) {
console.log('instance', msg);
}
}

class Child extends Parent {
static myMethod(msg) {
super.myMethod(msg);
}
myMethod(msg) {
super.myMethod(msg);
}
}
Child.myMethod(1); // static 1 这种调用方式是调静态方法,在类下直接调
var childs = new Child();
childs.myMethod(2); // instance 2
上面代码中,super在静态方法之中指向父类,在普通方法之中指向父类的原型对象。
*
* */


/*
* class A {
constructor() {
this.x = 1;
}
static print() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super();
this.x = 2;
}
static m() {
super.print();
}
}
B.x = 3;
B.m() // 3
上面代码中,静态方法B.m里面,super.print指向父类的静态方法。
这个方法里面的this指向的是B,而不是B的实例。
* */
原文地址:https://www.cnblogs.com/chaoyuehedy/p/10044122.html