ECMA5 got a new thing, that is, the user can object.defineproperty configuration properties can be written, configurable, enumerable , let us developers can define some properties, these properties are a bit like native bright
For example, we usually define an object this way;
var obj0 = { "nono"};
We can also use the new way, the object's property setting method DefineProperty Set the property, if the user does not pass enumberable, configurable, writable value, the default is False, This means that the default is not enumerable, cannot be configured, cannot be writable :
var obj1 ="name", { false, False,false, "nono"});
Writable
This configuration is not writable, so the name of the object OBJ1 is redefined invalid, (in the ECMA strict mode error);
varObj1 = {}; Object.defineproperty (Obj1,"name", {writable:false, Value:"Nono" }); Console.log ("My name is:"+obj1.name); //redefine the name;Obj1.name ="Qihao"; //Delete NameDelete obj1.name; Console.log ("my new name is:"+obj1.name); </script> </body>This is the result of the printout: the code that declares that we deleted and redefined the name does not take effect because writable is false;
We change the configuration of the element's writable from true to false, and then to True, the error
"Utf-8"/> varObj1 = {}; Object.defineproperty (Obj1,"Favor", {writable:true, Value:"Poppin" }); Object.defineproperty (Obj1,"Favor", {writable:false, Value:"Readbook" }); Try{ //If you redefine a writable property from false to True, an error is detected;Object.defineproperty (Obj1,"Favor", {writable:true, Value:"Poppin" }); }Catch(e) {Console.log ("Definedproperty Error"+e); } </script> </body>, because the default configurable is false, so reconfigure writable error;
ConfigurableNow the cofigurable comes in handy:
"Utf-8"/> varObj1 = {}; Object.defineproperty (Obj1,"Favor", {writable:true, Configurable:true, Value:"Poppin" }); Console.log (Obj1.favor); Object.defineproperty (Obj1,"Favor", {writable:false, Configurable:true, Value:"Readbook" }); Console.log (Obj1.favor); Try{ //because configurable is true, the redefinition of favor writable does not give an error;Object.defineproperty (Obj1,"Favor", {writable:true, Value:"Poppin" }); }Catch(e) {Console.log ("Definedproperty Error"+e); }; Console.log (Obj1.favor); </script> </body>The result is:
That is, by configuring configurable to True, you can change the enumerable,value at any time , the writable is configured to False or true is not a problem;
Enumerable"Utf-8"/> varObj1 = {}; Object.defineproperty (Obj1,"Favor", {enumerable:false, Value:"Poppin" }); Object.defineproperty (Obj1," Age", {value: - }); Object.defineproperty (Obj1,"Weight",{ "value": -, Enumerable:true }); for(varPinchobj1) Console.log (p); </script> </body>The weight attribute is output, and the attributes of favor and age are not enumerated;
Object.getownpropertydescriptorObject.getownpropertydescriptor can get a detailed description, but there is no native ...;
Object.defineproperties"Utf-8"/> varObj1 = {}; Object.defineproperties (obj1,{x: {value:"x"}, Y: {enumerable:true}, Z: {writable:true} }); for(varPinchobj1) Console.log (p); </script> </body>Multiple properties can be defined at once through defineproperties for quick and easy
accessor properties, get, set"Utf-8"/> varobj = {}; Object.defineproperty (obj,"name", { Set: function (name) { This. _name = name+"Afterfix"; }, Get: Function () {return "prefix"+ This. _name; } }); Obj.name="nnnn"; Console.log (Obj.name); </script> </body>Old Get,set, non-standard Getter,setter methodBefore the ECMA5 standard was adopted, most JS interpretation engines implemented non-standard get,set methods, which are now available under Chrome:
"Utf-8"/> varobj = {}; OBJ.__DEFINEGETTER__ ("g", function () {return This. _g+"__"; }); OBJ.__DEFINESETTER__ ("g", function (ARG) { This. _g =Arg; }); </script> </body> Output Result:
Property type: Data type, accessor type pit