JavaScriptの検出属性、属性特性、列挙属性

2113 ワード



/*

    

            

1.  in   

2.  hasOwnPerperty()                 false

3.  propertyIsEnumerable():                     true     true,            ,   JavaScript               。

*/



var o = { x: 1};



"x" in o.x; //true

"y" in o.x; //false

"toString" in o.x //true o  toString   





o.hasOwnProperty("x"); //true

o.hasOwnProperty("y"); //false

o.hasOwnProperty("toString") //false:toString       



o.propertyisEnumerable("x");  //true

o.propertyisEnumerable("y");  //false

Object.prototype.propertyIsEnumerable("toString");  //false



//    in     ,            “!==”         undefined



o.x !== undefined; //true

o.y !== undefined;  //false

o.toString !== undefined; //true







/*

    :

for/in                    (         ),           。            ,                   

*/





/*

    

         : (value)、   (writable)、    (enumerable)、    (configurable).

          : (get) 、   (set)、         



              ,ECMAScript5         "     "

       :value writrable enumerable configurable



    Object.getOwnPropertyDescription()(            )                  :



//   {value: 1, writrable:true, enumerable:true, configuration:true}

Object.getOwnPropertyDescrition({x:1}, "x");



             :  undefined



         ,              ,     Object.definProperty(),        、                     ;



Object.definProperty(o, "x", {x:1, writable: true, enumerable: false, configurable: true});



              ,     Object.defineproperties(),            ,           ,               ,          , :



var p = Object.defineProperties({},{

	x: {x:1, writable: true, enumerable: false, configurable: true},

	y: {x:1, writable: true, enumerable: false, configurable: true},

	r: {

		get: function() {return Math.sqrt(this.x*this.x + this.y*this.y) },

		enumerable: true,

		configrable: true

	}

});

*/