A new way to create JavaScript objects object.create () _javascript Tips

Source: Internet
Author: User
Tags access properties

What is Object.create ()?
object.create (proto [, Propertiesobject]) is a new object creation method proposed in E5, the first parameter is the prototype to inherit, if not a child function, you can pass a null, the second parameter is the object's property 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 function
var car = object.create (car.prototype);
Car.color = "Blue";
Alert (Car.getinfo ());

The result is:A blue undefined.

1, the detailed explanation of the Propertiesobject parameter: (the default is False)
Data properties:

    • Writable: Can write arbitrarily
    • Configurable: Whether it can be deleted, whether it can be modified
    • Enumerable: Can I enumerate with for
    • Value: Values

Access properties:

    • Get (): Access
    • Set (): Setting

2, Example: see the example directly to know how to use.

<! 
DOCTYPE html>  

The results are:
100
Yupeng
Yupeng1
201
I am Returning:yupeng
A Blue PORSCHE Boxter.
A Blue XXXXXXXX.
Aaaaaa

This is a new way of creating objects for JavaScript object.create (), I hope to help you learn.

Related Article

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.