判斷屬性是否存在於實例對象和原型對象中

// 判斷屬性是否存在於實例對象中 for-in


function Person(name, age) {
    this.name = name;
    this.age = 20;
}
var p = new Person();

console.log('name' in p) //true

//判斷一個屬性是否存在原型中

function hasPrototypeProper(object, name) {
    return !object.hasOwnProperty(name) && name in object;
}
相關文章
相關標籤/搜索