This article mainly introduces a new method for creating javascript objects, Object. create (). For more information, see
What is Object. create?
Object. create (proto [, propertiesObject]) is a new object creation method proposed in E5. the first parameter is the prototype to be inherited. If it is not a subfunction, you can pass a null parameter. The second parameter is the object's attribute descriptor. this parameter is optional.
For example:
function Car (desc) { this.desc = desc; this.color = "red";} Car.prototype = { getInfo: function() { return 'A ' + this.color + ' ' + this.desc + '.'; }};//instantiate object using the constructor functionvar car = Object.create(Car.prototype);car.color = "blue";alert(car.getInfo());
Result:A blue undefined.
1. Detailed description of the propertiesObject parameter: (both are false by default)
Data attributes:
- Writable: whether the data can be written at will
- Retriable: can it be deleted or modified?
- Enumerable: can be used for in enumeration?
- Value: value
Access attributes:
- Get (): Access
- Set (): set
2. Example: You can see how to use it directly.
Yupeng's document