当前位置:网站首页>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
边栏推荐
- Addition, deletion, modification and query of MySQL advanced table
- presto on spark 支持3.1.3记录
- How Axure installs a catalog
- Flomo software recommendation
- [SDU chart team - core] enumeration of SVG attribute class design
- How to play the guiding role of testing strategy
- 阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
- [leetcode refers to offer 22. The penultimate node in the linked list (simple)]
- Selenium displays webdriverwait
- The computer is out of power. How did I pass the terrible interview of Tencent cloud?
猜你喜欢
Lunch on the 23rd day at home
go interface
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
Question brushing plan - depth first search DFS (I)
Keywords static, extern + global and local variables
亚马逊和Epic将入驻,微软应用商城向第三方开放
Yolov5 NMS source code understanding
Deep understanding of modern mobile GPU (continuously updating)
Thinkphp5 + data large screen display effect
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
随机推荐
Win 11K in 100 days, super complete learning guide for job transfer test
引入结构化并发,Swift 5.5 发布!
Automatic heap dump using MBean
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Singleton mode
ROS learning notes - tutorial on the use of ROS
UKFslam
go interface
Deep understanding of modern mobile GPU (continuously updating)
Question brushing plan - depth first search (II)
thinkphp5+数据大屏展示效果
[leetcode refers to the substructure of offer 26. Tree (medium)]
unity 功能扩展
[leetcode refers to offer 42. Maximum sum of continuous subarrays (simple)]
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Recommended usage scenarios and production tools for common 60 types of charts
Leetcode-279-complete square number
ubutnu20安裝CenterNet