当前位置: 移动技术网 > IT编程>开发语言>JavaScript > typeof,instanceof的区别,扩展知识:显示原型(prototype)与隐式类型(__protot__)

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

2019年10月14日  | 移动技术网IT编程  | 我要评论

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网