当前位置:网站首页>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
边栏推荐
- 药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
- [pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
- Implementation of a small book system
- KVM 脚本管理 —— 筑梦之路
- Of binary tree_ Huffman tree_ Huffman encoding
- Module 5 (microblog comments)
- 解析數倉lazyagg查詢重寫優化
- Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment
- 汇编标志位相关知识点(连)
- MySQL 学习笔记
猜你喜欢

Three lines of code to simply modify the project code of the jar package

初始c语言时的一些知识

Drago Education - typescript learning

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

Solution to Nacos' failure to modify the configuration file mysql8.0

Of binary tree_ Huffman tree_ Huffman encoding

学习编程的起点。

爱可可AI前沿推介(6.25)

Maui's learning path (II) -- setting

坡道带来的困惑
随机推荐
1024 hydrology
Always maintain epidemic prevention and control and create a safe and stable social environment
KDD 2022 | GraphMAE:自监督掩码图自编码器
ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
Judge whether it is a mobile terminal
Sword finger offer day 1 stack and queue (simple)
KVM 脚本管理 —— 筑梦之路
字符串各操作函数与内存函数详解
Used in time filter (EL table)
.NET in China - What's New in .NET
Some knowledge about structure, enumeration and union
关于数据在内存中存储的相关例题
爱可可AI前沿推介(6.25)
关于一个图书小系统的实现
Sword finger offer II 025 Adding two numbers in a linked list
学习编程的起点。
[flask tutorial] flask development foundation and introduction
Download File blob transcoding
与生产环境中的 console.log 说再见