javascript: Creating objects using the Object.create () method

Source: Internet
Author: User

In JavaScript, we know that there are two common ways to create objects, one is to use the object's direct volume:

The object's direct quantity is a mapping table consisting of several value/key pairs, separated by a comma "," and the whole part is enclosed in curly braces "{}".

For example:

var empty={};
var people = {
  name: ' Kobe ', age
  : '% '
};
Another way is to create an object from new: Use a function call after new, which also becomes a constructor that initializes a new object by using new.

For example:

var arr = new Array ();
var date = new Date ();
var obj = new Object ();
In fact, JavaScript also defines a method in ECMAScript5 to create an object, Object.create (), and an argument can be an object.

For example:

var o = object.create ({x:1});

The prototype of the newly created object o is the parameter passed in Object.create (), and object o also has the attribute x.

Of course, can also create a common control object, similar to the effect of var o = {}, only need to change the parameters to Object.prototype, so that the creation of the object's prototype =object.prototype, in fact, is an instance of object.

var O1 = object.create (object.prototype);
var O2 = new Object;
var O3 = {};

As you can see from the figure above, these three methods create objects that are the same, that is, one of the most common object objects.

In addition, you can create a new object without a prototype by passing in the parameter null, which is not available in the first two ways.

var O4 = object.create (null);

The new object is pathetic, he doesn't have any prototypes, which means it doesn't have a built-in method, no ToString (), valueof, etc.





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.