JavaScript-Several ways to create objects

Source: Internet
Author: User

1. Factory mode
function Createperson (name, age, Job) {    varnew  Object ();     = name;     = Age ;     = job;     function () {        alert (this. Name);    };     return o;} var person1 = Createperson ("Nicholas", "Software Engineer"); var person2 = Createperson ("Greg", "Doctor");

The obvious disadvantage of this approach is the inability to know the type of object.

2. Constructor mode

function Person (name, age, Job) {    this. Name = name;      this. Age = Age ;     this. Job = job;      This function () {        alert (this. name);}    ;}

The disadvantage of this approach is that each Sayname method is recreated on each instance .

Each method is a function instance, equivalent to

This.sayname = new Function ("Alert (this.name)");

method should be the same for each person instance, so you can do this in a bad way (you might add a lot of global functions, and these global functions are only called by an object) to resolve:

function Person (name, age, Job) {    this. Name = name;      this. Age = Age ;     this. Job = job;     this. sayname = sayname;} function Sayname () {    alert (this. name);}
3. Prototype mode

Each function has a property that points to the prototype object, and the properties and methods in the prototype object are shared, so the properties and functions are not defined in the constructor method, but in the prototype object.

function= "Nicholas"="Software Engineer"function() {    alert (this. name);};

If you have to knock prototype every time too cumbersome, big can use the following way to replace:

function= {        "    Nicholas", "Software Engineer",     function  () {        alert (this. Name);    }};

One drawback of this approach is that the prototype object is a newly created object that overrides the original prototype object, so its constructor property no longer points to the person constructor, but instead to the object constructor. The type can only be judged by instanceof and cannot be judged by constructor. However, if the constructor property is important, it can also be set in the following way:

function= {    Constructor:person,    "Nicholas",    "     Software Engineer ",    function  () {        alert (this. Name);    }}; 

A new problem arises: the constructor property becomes enumerable, and if the JavaScript engine supports ES5, you can use Object.defineproperty () to reset the constructor.

If the properties and methods are all defined in the prototype, it is possible that there will be significant events. because the prototype object is shared, a change to one instance affects all instances of the property that contains the value of the reference type.

function= {    friends: ["AAA", "BBB"]}; var New Person (); var New Person ();p 1.friends.push ("CCC"); alert (p2.friends); // AAA,BBB,CCC
4. Combining the constructor pattern with the prototype mode

In fact, the above problem is mainly for each instance, the method is best shared, the attributes are independent. Then put the attribute in the constructor and place the method in the prototype.

function Person (name, age, Job) {    this. Name = name;      this. Age = Age ;     this. Job = job;     this. Friends = ["Shelby", "Court"= {    Constructor:person,    function  () {        alert (this. Name);    }}

This approach is most commonly used.

5. Dynamic Prototyping Mode

The definition object is divided into two parts: constructor and prototype, which may look less oo, so you can initialize the prototype in the constructor to make him more oo.

 function   person (name, age, job) { Span style= "COLOR: #008000" >//  this    . Name = name;     this . Age = age;     this . Job = job;  //  method   If  (typeof  this . Sayname! = " function ") {Person.prototype.sayName  = function<  /span> () {alert ( this  .name);    }; }}

You can not be lazy here do not write prototype, Xx.prototype = {BlaBla} The way each time the prototype object is rewritten.

6. Secure constructor function

An expert invented the concept of a safe object, that is, there is no public property, and can not use this to get anything. Used when security requirements are high, these environments prohibit the use of this and new.

function Person (name, age, Job) {    // Create object to return    varnew  object ();     // private variables and functions    can be defined here // Add Method    function () {        alert (name);    };     // return Object    return o;}

You can only access the Name property by using the Sayname () method.

JavaScript-Several ways to create objects

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.