当前位置:网站首页>JS prototype and prototype chain
JS prototype and prototype chain
2022-04-23 21:25:00 【Sister Chunfeng】
Constructor create object :
function Person() {
}
var person = new Person();
person.name = 'Kevin';
console.log(person.name) // Kevin
Person It's a constructor , We use new Created an instance object person
prototype
Each function has one prototype attribute
every last JavaScript object (null With the exception of ) Another object will be associated with it when it is created , This object is what we call prototype , Every object will be from prototype " Inherit " attribute .
function Person() {
}
// Though it's written in the notes , But you have to pay attention to :
// prototype It's a function that has properties
Person.prototype.name = 'Kevin';
var person1 = new Person();
var person2 = new Person();
console.log(person1.name) // Kevin
console.log(person2.name) // Kevin

proto
every last JavaScript object ( except null ) All have a property , It's called proto, This property points to the prototype of the object
function Person() {
}
var person = new Person();
console.log(person.__proto__ === Person.prototype); // true

constructor
Each prototype has a constructor Property points to the associated constructor The instance prototype points to the constructor
function Person() {
}
console.log(Person === Person.prototype.constructor); // true

function Person() {
}
var person = new Person();
console.log(person.__proto__ == Person.prototype) // true
console.log(Person.prototype.constructor == Person) // true
// By the way, learn a ES5 Methods , You can get the prototype of an object
console.log(Object.getPrototypeOf(person) === Person.prototype) // true
Examples and prototypes
function Person() {
}
Person.prototype.name = 'Kevin';
var person = new Person();
person.name = 'Daisy';
console.log(person.name) // Daisy
delete person.name;
console.log(person.name) // Kevin
In this case , Let's give the example object person Added name attribute , When we print person.name When , The natural result is Daisy.
But when we delete person Of name Attribute , Read person.name, from person Can't find name The attribute will be from person The prototype of person.proto , That is to say Person.prototype Search for , Fortunately we found name attribute , The result is Kevin.
Prototype and prototype
var obj = new Object();
obj.name = 'Kevin'
console.log(obj.name) // Kevin

Prototype chain
console.log(Object.prototype.__proto__ === null) // true

JavaScript The properties of the object are not copied by default , contrary ,JavaScript Just create an association between two objects , such , One object can delegate access to the properties and functions of another , So it's not inheritance , The statement of entrustment is more accurate
版权声明
本文为[Sister Chunfeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/110/202204200620163805.html
边栏推荐
- [leetcode refers to offer 52. The first common node of two linked lists (simple)]
- Reference of custom message in ROS function pack failed
- [leetcode refers to offer 18. Delete the node of the linked list (simple)]
- Detectron2 usage model
- Valueerror: invalid literal for int() with base 10 conversion error related to data type
- Thread safe sigleton (singleton mode)
- [leetcode refers to offer 42. Maximum sum of continuous subarrays (simple)]
- FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
- On the three paradigms of database design
- Centralized record of experimental problems
猜你喜欢

C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序

41. The first missing positive number

Zhongchuang storage | how to choose a useful distributed storage cloud disk

Problem brushing plan -- dynamic programming (III)

Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"

Another data analysis artifact: Polaris is really powerful

Resolve the "chromedriver executable needs to be in path" error

Express ③ (use express to write interface and cross domain related issues)

What if Jenkins forgot his password

Rust更适合经验较少的程序员?
随机推荐
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Detectron2 using custom datasets
Question brushing plan - depth first search (II)
laravel 发送邮件
Google 尝试在 Chrome 中使用 Rust
IOT 设计与开发
Centralized record of experimental problems
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
危机即机遇,远程办公效率为何会提升?
Addition, deletion, modification and query of advanced MySQL data (DML)
How to learn software testing? Self study or training? After reading this article, you will understand
[SDU chart team - core] enumeration of SVG attribute class design
Solve importerror: cannot import name 'imread' from 'SciPy misc‘
Sequential state
Another data analysis artifact: Polaris is really powerful
Automatic heap dump using MBean
Ubuntu 20 installing centernet
Yolov5 NMS source code understanding
Singleton mode