当前位置:网站首页>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
边栏推荐
- Summary of leetcode linked list problem solving skills
- 剑指offer 第 3 天字符串(简单)
- Openstack -- creating virtual machines for Nova source code analysis
- 始终保持疫情防控不放松 营造安全稳定的社会环境
- Solution to Nacos' failure to modify the configuration file mysql8.0
- leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
- 關於一道教材題的講解
- leetcode:456. 132 模式【单调栈】
- QT display ffmpeg decoded pictures
- Related examples of data storage in memory
猜你喜欢

Sword finger offer II 028 Flatten multi-level bidirectional linked list

剑指Offer 第 2 天链表(简单)

Implementation of a small book system

关于数据在内存中的存储下

解析數倉lazyagg查詢重寫優化

Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon

Summer Ending

Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream

À propos du stockage des données en mémoire

golang键盘输入语句scanln scanf代码示例
随机推荐
Online service emergency research methodology
Summer Ending
QT mouse tracking
Serenvlt first met
Native JS --- infinite scrolling
Nova中的api
JVM parameter interpretation
OpenStack学习笔记(二)
Component: is to switch between multiple components
leetcode:456. 132 模式【单调栈】
Accidentally modify or delete the system variable path to restore
提高排名的 15 个基本 SEO 技巧
Introduction to mongodb chapter 01 introduction to mongodb
Summer Ending
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
Openstack -- creating virtual machines for Nova source code analysis
MySQL learning notes
1251- client does not support authentication protocol MySQL error resolution
Qt显示FFmpeg解码的图片
leetcode - 384. Scramble array