A new method for creating javascript objects: Object. create (). javascript creates objects.
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.
<!DOCTYPE html>
Result:
100
Yupeng
Yupeng1
201
I am returning: YUPENG
A blue porsche boxter.
A blue XXXXXXXX.
Aaaaaa
The above is a detailed introduction to Object. create (), a new method for creating javascript objects. I hope it will help you learn more.
Articles you may be interested in:
- Introduction to the prototype of JavaScript design patterns (Object. create and prototype)
- Introduction to using Object. create () in JavaScript to create an Object