请说明 __proto__、prototype 的关系与属性查找规则,并对比 ES6 class 前后的继承方式。
核心规则:
prototype(原型对象),其 constructor 指回函数本身;__proto__(即 [[Prototype]]),指向创建它的构造函数的 prototype;__proto__ 逐级向上找 → 直到 Object.prototype.__proto__ === null 为止,这条链就是原型链。function Dog(name){ this.name = name; }
Dog.prototype.bark = function(){ return this.name + ': woof'; };
const d = new Dog('旺财');
d.bark(); // 自身没有 bark,沿原型链找到 Dog.prototype
d.toString(); // 继续向上找到 Object.prototype
继承方式演进:
题目与答案就在本页;收藏、批注和错题重练在应用内