Object. The create usage:
var obj = Object.create( prototype, [descriptors])
Creates a new object, assigns new attributes to the new object, and describes the attributes.
Obj2 = object. create(obj1, {name: {value: 'A', // Writable: false, // Supports any additional information that can be overwritten without any additional control. Enumerable: true}}Copy the code
Object. DefineProperties usage:
Add attributes to an object
var obj2 = { name: 'B', age: 22 } var sex = null; Object.defineProperties(obj2, { sex: { get: function() { return sex; } set: function(value) {this.sex = value; Sex = value; }}) obj2.sex = 'female ';Copy the code
The get function is called only when the object value is fetched for the extended property value. Set is called when it is reassigned, and this is the object itself
Object. DefineProperty usage:
var sex = null; DefineProperty (obj3, 'sex', {get: function() {return sex; } set: function(value) {sex = value}}) obj3.sex = 'female ';Copy the code
Methods on the object itself:
Var obj4 = {name: 'C', age: 42, get sex() {return 'male'}, set sex(value) {console.log(value); var obj4 = {name: 'C', age: 42, get sex() {return 'male'}, set sex(value) {console.log(value); }}; console.log(obj4.sex); Obj4. sex = 'female'Copy the code