面向对象的一些方法

2019-04-26  本文已影响0人  Yokiijay

prototype和_proto_

原型和原型链

function A(){
}
A.prototype.msg = 'I am A'
const a = new A()

instanceof

判断是否是某某的实例

function A(){}

const a = new A()
console.log( a instanceof A ) // => ture

in

判断属性有没有在对象或对象原型身上

function A(){
  this.name = 'yokiijay'
}
A.prototype.age = 21

const a = new ()

console.log( 'name' in a ) // true
console.log('age' in a) // true

hasOwnProperty

只判断对象身上有么有属性 (排除原型)

function A(){
  this.name = 'yokiijay'
}
A.prototype.age = 21

const a = new ()

console.log( a.hasOwnProperty('name') ) // true
console.log( a.hasOwnProperty('age') ) // false

isPrototypeOf

判断一个对象是否存在于另一个对象的原型链上

function A(){}

function B(){}
B.prototype = new A()

function C(){}
C.prototype = new B()

const c = new C()

console.log( A.prototype.isPrototypeOf(c) ) // true
上一篇 下一篇

猜你喜欢

热点阅读