Reference from the <<javascript authoritative guide to the 6th edition of >>, too much text is not easy to clarify the relationship, directly on the code and comments
1 /*2 * Object Setter and Getter properties:3 * defined as one or two functions with the same name as the property, this function uses get or set instead of function.4 * The role is very similar to the Getter/setter in Java. But they are not functions, so the calling method is more special.5 */6 varDemo = {7data:100,8 get Get_data () {9 return This. Data;Ten }, One set Set_data (data) { A This. data =data; - } - }; the Console.log (demo.get_data); -Demo.set_data = 200; - Console.log (demo.get_data); - + - //Ecmscript 5 Defines the object PropertyDescriptor object, which is used to implement the query and set operation of property attributes. +Console.log (Object.getownpropertydescriptor ({x:1}, "X"));//querying the own property descriptor A at /* - * Object.defineproperty (): Adds a property for an existing object or modifies its own property, but cannot modify the inherited property. - * If you need to modify multiple properties at the same time, you need to use Object.defineproperties (). - */ - - /*JS code creates objects by default: Readable, writable, configurable, enumerable, and extensible in * Extensible means: You can add new attributes. - * Method of Judgment: object.isextensible (obj); to * Prohibited mode: object.preventextensions (obj); + */ - //adds a non-enumerable variable x to the Obj object, so the output is {} the varobj = {}; *Object.defineproperty (obj, "x", {value:1, writable:true, Enumerable:false, Configurable:true}); $Console.log (obj);//"{}"Panax NotoginsengConsole.log ("X"inchOBJ);//"true", attribute x is present -Object.defineproperty (obj, "x", {writable:false}); theobj.x = 100;//no error, but nothing will be done. Throw TypeError in strict mode +Console.log (obj.x);//output: 1, not changed A //Modify properties by configuration x theObject.defineproperty (obj, "x", {value:2});//Modify the value of x, direct modification can not, through configuration modification is OK +Console.log (obj.x);//Output: 2 -Object.defineproperty (obj, "x", {get:function(){return100;} });//Modify attribute x to an accessor $Console.log (obj.x);//Output: $ /* - * Object.defineproperties () adds 3 properties directly to the object to be modified, and then returns to the new object R - */ the vart = {s:100}; - varp =object.defineproperties (t,{WuyiX:{value:1,writable:true, Enumerable:true, Configurable:true}, theY:{value:1,writable:true, Enumerable:true, Configurable:true}, - r:{ WuGetfunction(){ - returnMATH.SQRT ( This. x * This. x + This. Y * This. y); About }, $Enumerabletrue, -Configurable:true - } - }); AConsole.log (P);//{s:100, x:1, Y:1, R: [Getter]} +Console.log (P.R);//1.4142135623730951 theConsole.log (t);//{s:100, x:1, Y:1, R: [Getter]} The original object T has also been modified, like the P object - $ /* the * The Extend () function with JS comes with only simple copy of property name and value, cannot copy attribute and Getter/setter (simple conversion to static Data property) . the * deep copy below. the */ the Object.defineproperty (Object.prototype, -"Extend",//define Object.prototype.extend in { theWritable:true, theEnumerablefalse, AboutConfigurable:true, theValuefunction(obj) { the varnames = Object.getownpropertynames (obj);//get all of your own properties the for(vari=0;i<names.length;++i) { + if(Names[i]inch This) - Continue;//Skip an existing attribute the vardesc =Object.getownpropertydescriptor (Obj,names[i]);BayiObject.defineproperty ( This, Names[i],desc);//Copy the configuration of properties and properties. the } the } - }); - the varR ={}; the r.extend (p); theConsole.log (R);//properties and property configurations for p are copied. theConsole.log (Object.getownpropertydescriptor (R, "X")); - the /*to set the extensibility of an object: the *-The object cannot be transformed into extensible after it is converted to non-expandable. the *-Preventextensions only affects the object itself, and the newly added attributes on the prototype chain are also added to the non-extensible object.94 * More methods: Object.seal ()/object.issealed (), Object.freeze ()/object.isfreeze ( ) the */ theConsole.log (Object.isextensible (R));//"true" the object.preventextensions (r);98Console.log (Object.isextensible (R));//"false" About - /*101 * Serialization of Objects102 *-json.stringify () serializes an object's own property that can be enumerated.103 *-json.parse () Restore JS object104 */ the varobj = {X:1, y:{106z:[false,NULL,""]107 }};108 vars =json.stringify (obj);109 varT = Json.parse (s);//T is a deep copy of S the Console.log (s);111Console.log (t);
JavaScript Action Object Properties (set properties, Setter/getter, serialization)