原型 and 继承
原型链, prototypal 继承, and 如何对象链接 and share behavior.
你将学到
- 什么原型链 是 and 如何属性 lookup 工作
- 每个对象有 __proto__ 链接到 它的原型
- Constructor 函数 and 原型属性
- 对象.创建() for explicit prototypal 继承
- 如何 ES6 类是 syntactic sugar over 原型
概念讲解
The Prototype Chain
每个JavaScript object has an internal link to another object called its prototype (accessed via 对象.getPrototypeOf() or the __proto__ property). When you access a property that does not exist on the object itself, JavaScript walks up this prototype chain until it finds the property or reaches the end (空). This is how inheritance works in JavaScript — it is delegation, not copying.
Constructor Functions
Before ES6 classes, objects were created with constructor functions. When you call a function with 新, JavaScript creates a fresh object, sets its prototype to the function's 原型 property, calls the function with 这个 bound to the new object, and returns it. Methods shared by all instances are placed on Constructor.原型 so they are stored once, not copied to every instance.
Object.create()
对象.创建(proto) creates a new object whose prototype is explicitly the argument you pass. This is the purest form of prototypal inheritance — you pick the prototype directly instead of going through a constructor. Pass 空 to create an object with no prototype (a truly empty dictionary).
The Chain in Practice
The chain typically looks like: your object → Constructor.原型 → 对象.原型 → 空. Methods like hasOwnProperty, toString, and valueOf live on 对象.原型 and are available to every object through the chain. Understanding this explains why [] has 推送 (from 数组.原型) and why everything has toString.
Classes Are Sugar
ES6 类 syntax does not introduce a new inheritance model — it is syntactic sugar over the same prototype system. 类 methods are placed on the prototype, extends sets up the prototype chain, and super walks up it. Knowing the underlying prototypes helps you debug, inspect, and understand why certain patterns work the way they do.
示例代码
// Constructor function with shared prototype methods
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
Animal.prototype.describe = function () {
return `Animal: ${this.name}`;
};
function Dog(name, breed) {
Animal.call(this, name); // call super constructor
this.breed = breed;
}
// Set up inheritance: Dog.prototype inherits from Animal.prototype
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
return `${this.name} says Woof!`;
};
const rex = new Dog("Rex", "Labrador");
console.log(rex.speak()); // "Rex makes a sound" — from Animal.prototype
console.log(rex.bark()); // "Rex says Woof!" — from Dog.prototype
console.log(rex.describe()); // "Animal: Rex" — inherited
// Inspect the prototype chain
console.log(Object.getPrototypeOf(rex) === Dog.prototype); // true
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
// Object.create for a plain prototype link
const proto = { greet() { return "hi"; } };
const obj = Object.create(proto);
console.log(obj.greet()); // "hi" — delegated to proto
Shows constructor 函数使用原型方法, manual 继承设置使用对象.创建, instanceof 检查, 原型链 inspection, and 对象.创建 for direct prototypal 链接.
动手试一试
- JSON 树 Viewer
Inspect 对象结构 and 它们的 inherited 属性.
- JSON Formatter
格式对象到 see 它们的 own enumerable 属性.
常见错误
错误写法
Assigning 方法内部 constructor 相反的 on 原型
正确做法
方法定义内部 constructor 是 recreated for 每个实例, wasting memory. 位置 shared 方法 on Constructor.原型所以所有实例 share one 复制通过原型链.
错误写法
Forgetting 到 reset constructor 属性之后 reassigning 原型
正确做法
之后 Dog.原型 = 对象.创建(Animal.原型), constructor 指针是 错误. Reset 它使用 Dog.原型.constructor = Dog 所以 instanceof and introspection 工作正确地.
相关资源
相关速查表
- JavaScript Cheatsheet
原型链, constructor 模式, and 继承.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。