Open CPPAlien opened 4 years ago
class 是个 function 的语法糖,默认使用 use strict (this 指针不默认指向全局)。里面的方法,相当于在这个 Function 的 prototype 中,而字段相当于挂在这个 Function 的 this 上。new 在 class 上创建对象的过程,相当于执行在这个函数的 constructor 的过程,其他同直接在函数上的 new。
class test {
print() {
this.realPrint()
}
realPrint() {
console.log('xxxx')
}
}
const a = new test();
a.print(); // 正确,因为进行了隐式绑定
const {print} = a;
print(); // 报错
class Index {
constructor() {
}
print() {
console.log('aa')
}
}
console.log(new Index().print()) // 'aa'
当 constructor 有返回时,会报错
class Index {
constructor() {
return {
}
}
print() {
console.log('aa')
}
}
console.log(new Index().print()) // TypeError: (intermediate value).print is not a function
可以在 constructor 中 使用 this.print = this.print.bind(this) 的方法,或者把 print 写成箭头函数。
Function 本身是一个对象,当一个函数对象被创建时,新函数对象被赋予一个 prototype 属性,值是包含一个 constructor 属性并且属性值为该新函数的对象。
所以当执行 new 获得一个新对象后,新对象的 constructor 也依然指向函数本身;
let Person = function() {}
let p = new Person()
p.constructor === Person // true
当代码 new Foo(...) 执行后,会又以下操作。
用闭包实现 private 属性