1.Object.keys(..)會返回一個數組,包含所有可枚舉屬性( enumerable: true)
2.Object.getOwnPropertyNames(...)會返回一個數組,包含所有屬性,無論它們是否可枚舉
注:Object.keys(..)和Object.getOwnPropertyNames(..)都只會查找對象直接包含的屬性。
3.in操作符會檢查屬性是否在對象及其[[Prototype]]原型鏈中
4.hasOwnProperty(..)只會檢查屬性是否在對象中,不會檢查[[Prototype]]鏈
var Obj = {
name: 'mini',
age: 3,
show: function () {
console.log(this.name + " is " + this.age);
}
}
//MyObj 繼承obj, prototype指向Obj
var myObject = Object.create(Obj, {
like: {
value: "fish", // 初始化賦值
writable: true, // 是否是可改寫的
configurable: true, // 是否能夠刪除,是否能夠被修改
enumerable: true //是否可以用for in 進行枚舉
},
hate: {
configurable: true,
get: function () { console.log(111); return "mouse" },
// get對象hate屬性時觸發的方法
set: function (value) {
// set對象hate屬性時觸發的方法
console.log(value, 2222);
return value;
}
}
});
console.log("like" in myObject); // true
console.log("age" in myObject); // true
console.log(myObject.hasOwnProperty("like")) // true
console.log(myObject.hasOwnProperty("age")); // false