Summary of Javascript Creation object methods

Source: Internet
Author: User
Tags naming convention

Recently read the "JavaScript Advanced Programming (Third Edition)", this book is recommended by many people, I also solemnly recommend again. After watching it, I have to summarize it, so I chose a theme to share with you.

There are many ways to create objects using JavaScript, and now you can enumerate them:

1. Using the object constructor to create an object, the following code creates a person object and prints out the property value of name in two ways.

    var person = new Object ();    Person.name= "Kevin";    person.age=31;    alert (person.name);    Alert (person["name"])

2. Create an object using the object literal; do not be surprised person["5"], here is legal, in addition to the use of this type of parentheses between the fields can have spaces, such as person["my Age"].

    var person =     {        name: "Kevin",        age:31,        5: "Test"    };    alert (person.name);    Alert (person["5"]);

3. Create an object using the Factory mode and return the person object with properties and methods.

function Createperson (name, age,job) {    var o = new Object ();    O.name=name;    o.age=31;    O.sayname=function ()    {        alert (this.name);    };    return o;} Createperson ("Kevin", +, "se"). Sayname ();

4. Create an object using a custom constructor pattern; Note the naming convention, which is the first letter of the function as a constructor, to distinguish other functions. One flaw in this approach is sayname, where each instance points to a different function instance rather than the same one.

function Person (name,age,job) {    this.name=name;    This.age=age;    This.job=job;    This.sayname=function ()    {        alert (this.name);    };} var person = new Person ("Kevin", +, "SE");p erson.sayname ();

5. Create the object using the prototype mode, and solve the flaw mentioned in Method 4, so that functions of different objects (such as sayfriends) point to the same function. But it also has a flaw, that is, the instance shares the reference type friends, from the following code execution results can be seen, two instances of the friends value is the same, this may not be what we expected.

function person () {}person.prototype = {    Constructor:person,    name: "Kevin",    age:31,    job: "SE",    friends:["Jams", "Martin",    sayfriends:function ()    {        alert (this.friends);    }}; var person1 = new Person ();p Erson1.friends.push ("Joe");p erson1.sayfriends ();//jams,martin,joe

Person2.sayfriends ();//james,martin,joe

6. Combining the use of prototype patterns and constructors to create objects solves the flaws mentioned in method 5, and this is the most widely used and most recognized method of creating objects.

function Person (name,age,job) {    this.name=name;    This.age=age;    This.job=job;
this.friends=["Jams", "Martin"]; Person.prototype.sayfriends=function () { alert (this.friends);}; var person1 = new Person ("Kevin", +, "SE");
var person2 = new Person ("Tom", "a", "SE");
Person1.friends.push ("Joe");p erson1.sayfriends ();//jams,martin,joe
Person2.sayfriends ();//jams,martin

7. Dynamic prototyping mode; the benefit of this pattern is that it looks more like traditional object-oriented programming and has better encapsulation because it completes the prototype creation in the constructor. This is also a recommended method of creating objects.

function Person (name,age,job) {    //attribute    This.name=name;    This.age=age;    This.job=job;    this.friends=["Jams", "Martin";    Method    if (typeof this.sayname! = "function")    {        person.prototype.sayname=function ()        {            alert ( this.name);        };                Person.prototype.sayfriends=function ()        {            alert (this.friends);}        ;}    } var person = new Person ("Kevin", +, "SE");p erson.sayname ();p erson.sayfriends ();

There are also two methods for creating objects, parasitic constructor patterns and secure constructor patterns. Since these two functions are not particularly common, no specific code is given here.

Write so many methods of creating objects, in fact, the real recommendation is the method 6 and Method 7. Of course, in the real development to choose according to the actual needs, perhaps created objects do not need a method, there is no need to choose them.

Summary of Javascript Creation object methods

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.