prototype
你以前可能经常看到,比如在 ES5 中创建一个构造函数或者类的时候:
function People(name) {
this.name = name
}
People.prototype.greet = function () {
return 'hello ' + this.name
}
然后你就可以用这个构造函数创建一个对象:
var kevin = new People('kevin')
kevin.greet()
// 会输出 hello kevin
这一系列内部流程大概是:
People
,他是继承自 Function
这个构造函数,也就带有 prototype
属性kevin
,这个对象上的 __proto__
是根据构造函数上的 prototype
创建的(prototype
存在于构造函数上,而 __proto__
存在于任何地方).greet
方法,它会先搜索这个对象自己的属性 kevin.greet
然后找不到继续搜索 kevin.__proto__.greet
这个从构造函数创建的方法。类似的:
var foo = {bar: 1}
console.log(foo.__proto__)
这里的 foo.__proto__
就是从 Object.prototype
创建的对象
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too