JavaScript a new way to create objects-object.create ()

Source: Internet
Author: User
Tags access properties

What is 1.object.create ()?

Object.create (Proto [, Propertiesobject]) is a new way of creating objects in E5, the first parameter is the prototype to inherit, if not a child function, you can pass a null, the second argument 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 Functionvar car =  object.create (car.prototype); car.color = "Blue"; alert ( Car.getinfo ());

The result is: A blue undefined.

Detailed explanation of 2.propertiesObject parameters: (False by default)

Data properties

    • Writable: Is it possible to write any
    • Configurable: Whether it can be deleted, whether it can be modified
    • Enumerable: Can I enumerate with for in
    • Value: Values

Access properties:

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

3. Example: Just look at examples and know how to use them.

<! DOCTYPE html>

The result is:

-Yupeng
yupeng1
201
I am Returning:yupeng
A Blue PORSCHE boxter.
A Blue XXXXXXXX.
aaaaaaReference article: 1.http://www.htmlgoodies.com/beyond/javascript/ object.create-the-new-way-to-create-objects-in-javascript.html2.http://www.jimmycuadra.com/posts/ ecmascript-5-object-creation-and-property-definition3.http://msdn.microsoft.com/zh-cn/library/ie/ff925952 (v= vs.94). aspx

JavaScript a new way to create objects-object.create ()

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.