[[Prototype]] : Object
时间: 2023-10-24 13:06:00 浏览: 49
[[Prototype]] is a property that exists on all objects in JavaScript. It is an internal link to another object, known as the object's prototype. In other words, every object in JavaScript inherits properties and methods from a prototype object, which is used as a blueprint for the object's behavior.
When a property or method is called on an object, JavaScript first checks if the object itself has that property or method. If it doesn't, it looks up the prototype chain to see if the object's prototype has it. This prototype chain continues until the root object, Object.prototype, is reached.
For example, consider the following code:
```
let person = {
name: "John",
age: 30,
sayHello: function() {
console.log("Hello!");
}
};
let student = {
major: "Computer Science"
};
student.__proto__ = person;
console.log(student.name); // "John"
student.sayHello(); // "Hello!"
```
In this example, we create a person object with a name, age, and sayHello method. We then create a student object and set its prototype to be the person object. This means that the student object will inherit the properties and methods of the person object.
When we try to access the `name` property of the student object, JavaScript first checks if the student object has a `name` property. Since it doesn't, it looks up the prototype chain and finds the `name` property on the person object. Similarly, when we call the `sayHello` method on the student object, JavaScript looks up the prototype chain and finds the `sayHello` method on the person object.
Overall, [[Prototype]] is a powerful feature in JavaScript that allows for efficient and flexible object inheritance.