枚举属性and封装检测对象的公有属性

枚举属性

可枚举属性

  • 对象的私有属性
  • 给类上面新增扩展的属性
  • 私有属性和原型上新增的属性都是可枚举的;

不可枚举

  • 对象或原型中天生自带的属性属于不可枚举属性
        // 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章