Skip to content

原型

一句话解释

原型 是 对象 那个 other 对象 inherit 属性 and 方法 从, forming 原型 链.

用大白话说

每个JavaScript object has a prototype — another object that it inherits properties and methods from. When you access a property, JavaScript checks the object first, then its prototype, then the prototype's prototype, and so on up the chain until it finds the property or reaches null.

这个is called the prototype chain, and it is how inheritance works in JavaScript. Unlike class-based languages, JavaScript uses prototypal inheritance — objects inherit directly from other objects.

ES6 类 syntax is syntactic sugar over prototypes. When you write a method in a class, it is added to the class's prototype object. All instances share the same prototype methods, which is memory-efficient.

示例

javascript
const animal = { eat() { return "eating"; } };
const dog = Object.create(animal);
dog.bark = () => "woof";
console.log(dog.eat());  // "eating" — inherited from prototype
console.log(dog.bark()); // "woof" — own property

dog inherits eat 从 它的 原型 (animal) and 有 它的 own bark 方法.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表