class 内函数定义区别

class A {

say() {

console.log('i am A');

}

}

const a1 = new A();

const a2 = new A();

console.log(a1.__proto__.say); // f say()

console.log(a1.say === a2.say); // true

class B {

say = function() {

console.log('i am B');

}

}

const b1 = new B();

const b2 = new B();

console.log(b1.__proto__.say); // undefined

console.log(b1.say === b2.say); // false

原来,换种写法就上不了原型链了。

babel 结果:Babel · The compiler for writing next generation JavaScript

以上是 class 内函数定义区别 的全部内容, 来源链接: utcz.com/z/264553.html

回到顶部