javascript: Creating Objects with 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 direct amount of objects:

The direct amount of an object is a mapping table consisting of several value/key pairs separated by a comma "," and the entire section enclosed in curly braces "{}".

For example:

var empty={};   var = {    name:' Kobe ', age    :' People '  };  

Another way is to create an object with new: A function call is used after new, and the function is also a constructor that initializes a new object with a.

For example:

var New Array ();   var New Date ();   var New Object ();  

In fact, JavaScript also defines a method in ECMAScript5 to create an object,object.create (), and the argument can be an object .

For example:

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

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

Of course, you can also create a normal object, similar to the effect of var o = {}, only need to change the parameter to Object.prototype, let create object prototype =object.prototype, in fact, is an instance of object.

1 var o1 = object.create (object.prototype);   2 var New Object;   3 var o3 = {};  

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

In addition, it is possible to create a new object without a prototype by passing in the parameter null , which is not available in the first two methods.

var o4 = object.create (null);  

This new object is pathetic, he does not have any prototype, that is, it does not have a method built into the object, not ToString (), valueof and so on.

"Reprint" http://blog.csdn.net/hutaoer06051/article/details/7983316

javascript: Creating Objects with the Object.create () method

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.