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

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

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

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