当前位置:网站首页>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 : undefined
string
boolean
number
symbol(ES6)
Object
Function
) 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
边栏推荐
- Implementation of a small book system
- Solution to Nacos' failure to modify the configuration file mysql8.0
- 剑指offer 第 3 天字符串(简单)
- [pit avoidance means "difficult"] actionref current. Reload() does not take effect
- 药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
- The starting point for learning programming.
- Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
- 剑指 Offer II 029. 排序的循环链表
- Used in time filter (EL table)
- [pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
猜你喜欢
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
指针,它那些不得不说的题目
[machine learning] model and cost function
數據在內存中的存儲相關內容
关于结构体,枚举,联合的一些知识
Capabilities required by architects
Golang keyboard input statement scanln scanf code example
Storage related contents of data in memory
KDD 2022 | graphmae: self supervised mask map self encoder
随机推荐
Explanation of a textbook question
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
三行代码简单修改jar包的项目代码
Uncover gaussdb (for redis): comprehensive comparison of CODIS
关于数据在内存中的存储下
Solve the problem that yarn cannot load files in vs Code
Custom vertical table
KVM script management - the road to dream
leetcode:剑指 Offer II 091. 粉刷房子【二维dp】
Where is it safe to open an account for buying funds? Please give me your advice
leetcode - 384. 打乱数组
始终保持疫情防控不放松 营造安全稳定的社会环境
关于一道教材题的讲解
Leetcode: Sword finger offer II 091 Painting house [2D DP]
Solution to Nacos' failure to modify the configuration file mysql8.0
Summer Ending
LeetCode链表题解技巧归纳总结
OpenStack学习笔记-Glance组件深入了解
The starting point for learning programming.
Serevlt初识