当前位置:网站首页>Prototype and prototype chain - constructor and instanceof
Prototype and prototype chain - constructor and instanceof
2022-06-25 13:22:00 【wendyTan10】
instanceof Determine the value type
Type judgment
typeof Is used to determine all value types ,( Return data type : undefinedstringbooleannumbersymbol(ES6)ObjectFunction) Can identify reference types , But you can't distinguish object And Array The type of
var list = [];
console.log(typeof list); // object
How to determine the type of array , Use instanceof:instanceof The essence of is detection Prototype chain , Operators are used to determine the type of value .instanceof Operator returns a Boolean value boolean, Indicates whether an object is an instance of the specified constructor .
var d = new Date();
d instanceof Date // true
d instanceof Object // true
notes : about undefined and null,instanceOf Operator always returns false.
undefined instanceof Object // false
null instanceof Object // false
Constructors
Constructors It is created in the same way as normal functions , It's customary to capitalize ; Used to create a new instance object
function Person(name){
this.name=name;
this.sayHi = function() {
alert("Hi")
}
}
var student1= new Person('wendy');
var student2= new Person('kim');
student1.name; // 'wendy'
student1.sayHi(); //
** shortcoming :** Between multiple instances of the same constructor , Can't share properties , So as to cause the waste of system resources .
prototype Attribute function of prototype
JavaScript Each object of the object inherits another object , The latter is called “ Prototype ”(prototype) object . One side , Any object , Can be used as the prototype of other objects ; On the other hand , Because the prototype object is also an object , So it has its own prototype .null It can also serve as a prototype , The difference is that it doesn't have its own prototype object .
Each constructor has one prototype attribute , This property will be used when generating instances , Become the prototype object of the instance object .JavaScript The design of inheritance mechanism is , All properties and methods of the prototype , Can be shared by child objects ; Inheritance mechanism of prototype chain ;
constructor Properties of
prototype The object has a constructor attribute , The default point to prototype The constructor where the object resides
because constructor Property defined in prototype The above object , It means that it can be inherited by all instance objects .
function P() {
}
P.prototype.constructor === P // true
var p = new P();
// function P() {}
p.constructor === P.prototype.constructor
// true
p.hasOwnProperty('constructor')
// false
Click to enter : Introduction to prototype and prototype chain
边栏推荐
- 关于扫雷的简易实现
- API in Nova
- Confusion caused by the ramp
- Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment
- Some knowledge about structure, enumeration and union
- 药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
- Explication d'un problème de manuel
- How unity makes the UI intercept click events
- 《MongoDB入门教程》第01篇 MongoDB简介
- 指针,它那些不得不说的题目
猜你喜欢
随机推荐
Qt显示FFmpeg解码的图片
OpenStack学习笔记(二)
Intercept based on byte length
Seven competencies required by architects
Three lines of code to simply modify the project code of the jar package
MySQL 学习笔记
解析數倉lazyagg查詢重寫優化
Pointer, it has to say that the subject
Solve the problem that yarn cannot load files in vs Code
关于一道教材题的讲解
剑指 Offer II 032. 有效的变位词
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
AGCO AI frontier promotion (6.25)
15 basic SEO skills to improve ranking
初始c语言时的一些知识
Optimization of lazyagg query rewriting in parsing data warehouse
关于一个图书小系统的实现
[pit avoidance means "difficult"] actionref current. Reload() does not take effect
时间过滤器(el-table)中使用
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default









