A new method for creating javascript objects: Object. create (). javascript creates objects.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.