Understanding Object Prototypes and Prototype Chains in JavaScript

This article will introduce you to object prototypes and prototype chains in Javascript. There is a certain reference value, and friends in need can refer to it, and I hope it will be helpful to everyone. Object prototype I believe everyone has used map like this: let arr = [0, 1, 2] let doubleArr = arr. map(c => c * 2) console.log(doubleArr) // 0, 2, 4 I don’t know if you have thought about it, arr itself does not set the map attribute , then why can we use the map function? Print it out to see: console.log(arr) // 0: 0 // 1: 1 // twenty two // length: 3 // __proto__: Array(0) There is an object named __proto__, if you expand it, you will see all the functions that can be used by Array objects; of course We can also find the map function in it, and this is the arr.map function called in the example: console.log(arr.map === arr.__proto__.map) // true The __proto__ object that appears here is the so-called Prototype. Different from Java, C# and other class-based (Class) object-oriented languages, properties and methods are passed by defining classes, creating instances, specifying inheritance, etc.; Javascript is a Prototype-based pairing…

Understanding Object Prototypes and Prototype Chains in JavaScript

This article will introduce you to object prototypes and prototype chains in Javascript. There is a certain reference value, and friends in need can refer to it, and I hope it will be helpful to everyone. Object prototype I believe everyone has used map like this: let arr = [0, 1, 2] let doubleArr = arr. map(c => c * 2) console.log(doubleArr) // 0, 2, 4 I don’t know if you have thought about it, arr itself does not set the map attribute , then why can we use the map function? Print it out to see: console.log(arr) // 0: 0 // 1: 1 // twenty two // length: 3 // __proto__: Array(0) There is an object named __proto__, if you expand it, you will see all the functions that can be used by Array objects; of course We can also find the map function in it, and this is the arr.map function called in the example: console.log(arr.map === arr.__proto__.map) // true The __proto__ object that appears here is the so-called Prototype. Different from Java, C# and other class-based (Class) object-oriented languages, properties and methods are passed by defining classes, creating instances, specifying inheritance, etc.; Javascript is a Prototype-based pairing…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索