首页 > 其他 > 详细

typeof,instanceof的区别,扩展知识:显示原型(prototype)与隐式类型(__protot__)

时间:2019-10-14 12:35:26      阅读:86      评论:0      收藏:0      [点我收藏+]

3.typeof 和instanceof区别

1.typeof

主要用于判断对象类型

console.log(typeof null)          //object
console.log(typeof undefined)     //undefined
console.log(typeof [1,2,3])       //object
console.log(typeof Boolean)       //function
console.log(typeof 1)             //number
console.log(typeof ‘1‘)           //string
console.log(typeof String)        //function
console.log(typeof boolean)       //undefined
console.log(typeof true)          //boolean
console.lig(typeof symbol)        //symbol
console.log(typeof Function)      //function

 

类型有:

1.object

2.function

3.number

4.string

5.boolean

6.undefined

7.symbol =>一种标识唯一性的ID

注意:每个symbol属性都是唯一的,任意两个symbol都不相等

2.instanceof

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

右边必须为一个对象

console.log(typeof null)          //object
console.log(typeof undefined)     //undefined
console.log(typeof [1,2,3])       //object
console.log(typeof Boolean)       //function
console.log(typeof 1)             //number
console.log(typeof ‘1‘)           //string
console.log(typeof String)        //function
console.log(typeof boolean)       //undefined
console.log(typeof true)          //boolean
console.lig(typeof symbol)        //symbol
console.log(typeof Function)      //function

 

注意:每个函数的原型链上都有原型,再上面都有对象

1.显示原型 : prototype

只要创建一个新的函数,就会为该函数创建一个prototype属性.该属性指向函数的原型对象.所有原型对象都会自动获得一个constructor构造函数属性,该属性指向prototype属性所在函数的指针.

2.隐式原型 : __proto__

隐式原型指向创建这个对象的函数的prototype.

object.peototype.__proto__ == null

 

注意:通过Function.prototype.bind方法构造出来的函数没有prototype属性。

3.显示原型与隐式原型的区别和联系
function P(){}
let p1 = new P();
p1.__proto__ === P.prototype //true
P.prototype  //  {constructor: ƒ}
P.prototype.constructor === P //true
P.__proto__ === Function.prototype //true

 

1.对象只有__proto__属性,这个属性是指向他的构造函数的prototype属性

function B(b){
    this.b = b;
}
var b = new B(‘seanxiao‘)
console.log(b)

 

b对象的属性是__proto__,指向构造函数B的prototype,B.protptype.__proto__指向Object.prototype

Object.protptype.__protp__是null

而则一层一层的链接 关系就是原型链。

typeof,instanceof的区别,扩展知识:显示原型(prototype)与隐式类型(__protot__)

原文:https://www.cnblogs.com/crushxz/p/11670481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!