枚舉屬性
可枚舉屬性
不可枚舉
- 對象或原型中天生自帶的屬性屬於不可枚舉屬性
// var obj = {x:1,y:2,z:3};
// for(var key in obj){
// console.log(key);
// }
// Object.prototype
// function Fn(){
// this.a=100;
// this.b=200;
// }
// Fn.prototype.getX=function(){
// }
// Fn.prototype.y=function(){
// }
// var f = new Fn;// {a:100,b:200}
// // for in 可以列舉出所有的可枚舉屬性
// for(var key in f){
// console.log(key);
// }
// 可枚舉屬性: 1.對象的私有屬性 2.給類上面新增擴展的屬性
// 不可枚舉:對象或原型中天生自帶的屬性屬於不可枚舉屬性;
檢測一個屬性是否是自己的私有屬性
- hasOwnProperty用來檢測一個屬性是否是自己的私有屬性,是就返回true,反之false
- 實例.hasOwnproperty(被檢測的值)
- hasOwnproperty在Object的原型上
let ary = [1,2,3];
console.log( ary.hasOwnProperty(0)) // true 0是ary實例的私有屬性
console.log( ary.hasOwnProperty('push')) // fasle push是ary實例的公有屬性
----------------------------------------------------------------------------------
// hasOwnProperty : 如果是私有的,返回true;如果是公有或者不是其屬性,都返回false;
// Array.prototype.Mypop=function(){
// }
// var ary = [1,2,3];// pop
// for(var key in ary){
// if(ary.hasOwnProperty(key)){// 如果是私有返回true;
// console.log(key);
// }
// }
// console.log(ary);
var obj = {a:1};
console.log(obj.hasOwnProperty("b")) //false
檢測一個屬性是否屬於某個對象
- in用來檢測一個屬性是否屬於某個對象
- 私有公有都返回true
var obj = {a:1};
console.log("a" in obj); //true
console.log("hasOwnProperty" in obj);//true
封裝檢測一個屬性是否是對象的公有屬性
- 先用in看一下是不是自己的屬性,如果是再用hasOwnProperty檢測一下是不是自己的私有屬性,那剩下的就是公有屬性了
function Fn(){
this.a=100;
this.b=200;
}
Fn.prototype.getX=function(){
}
Fn.prototype.y=function(){
}
Fn.prototype={
x:1
}
var f = new Fn;
//檢測公有
// function hasPublicProperty(obj, attr) {
// // return (obj.hasOwnProperty(attr)) ? false:attr in obj ;
// // return !obj.hasOwnProperty(attr)&&attr in obj ;
// return attr in obj.__proto__;
// }
// console.log( hasPublicProperty(f, "getX"));
----------------------------------------------------------------------------------
// 判斷該屬性是否是公有屬性;如果是返回true,不是返回false;
// 1. 確保attr是obj的一個屬性 in
// 2. 確保不是私有屬性;
// 先有思路,後有代碼;
function hasPublicProperty(obj,attr){
// if(attr in obj){// 是attr是obj的屬性
// if(!obj.hasOwnProperty(attr)){// 保證不是私有屬性
// return true;
// }else{
// return false
// }
// }else{
// return false;
// }
return attr in obj && !obj.hasOwnProperty(attr)
}
console.log(hasPublicProperty(f,"x"))
-----------------------------------------------------------------------------
let ary = [1, 2, 3];
console.log(ary.hasOwnProperty(0)) // true 0是ary實例的私有屬性
console.log(ary.hasOwnProperty('push')) // fasle push是ary實例的公有屬性
// 檢測一個屬性是否是對象的公有屬性
/*
先用in看一下是不是他的屬性,如果是在用hasOwnProperty檢測一下是不是自己的私有屬性,
那剩下的就是公有屬性了
*/
Object.hasPubProperty = function (value) {
// 1.先檢測屬性名對不對
let ary = ['number', 'string']; // 這裏存放的是符合規範的屬性名的數據類型
// typeof value的返回結果是字符串類型的數據類型,看這個數據類型在ary數組裏有沒有,
如果有用includes檢測就是true,反之就是false
if (!ary.includes(typeof value)) {
return false
}
// 誰執行這個方法this就是誰
let n = value in this; // 檢測是否是對象的屬性
let m = this.hasOwnProperty(value); // 檢測是否是對象的私有屬性
return n && !m; // 如果是自己的屬性而且還不是私有屬性,那就是公有屬性
}
console.log(ary.hasPubProperty('push')) // true
console.log(ary.hasPubProperty(0)) // fasle
console.log(ary.hasPubProperty([])) // false
console.log(Array.prototype.hasPubProperty('push')) // false
console.log(Object.hasPubProperty('hasOwnProperty')); // false
console.log(Object.hasOwnProperty('hasOwnProperty')); // true