Several modes of creating objects in JS

/**
 * Created by sibuk on 2017/6/5.
 */

//factory pattern
function createPerson( name, age, job ) {
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function() {
        alert(this.name);
    }

    return o;
}

var person1 = createPerson("Nicholas", 29, "software Engineer");
var person2 = createPerson("Greg", 27, "Doctor");

person1.sayName(); //"Nicholas"
person2.sayName(); //"Greg"

// Constructor mode
function Person( name, age, job ) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function() {
        alert(this.name);
    }
}

var person1 = new Person("Nicholas", 29, "software Engineer");
var person2 = new Person("Greg", 27, "Doctor");

person1.sayName(); //"Nicholas"
person2.sayName(); //"Greg"

// prototype mode
function Person(){

}

Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "software Engineer";
Person.prototype.sayName = function() {
    alert(this.name);
}

var person1 = new Person ();
person1.sayName(); //"Nicholas"

var person2 = new Person ();
person2.sayName(); //"Nicholas"

alert(person1.sayName == person2.sayName); //true

//Combination of Constructor Pattern and Prototype Pattern
function Person( name, age, job ) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.friends = ["Shelby","Court"];
}

Person.prototype = {
    constructor: Person,
    sayName: function(){
        alert(this.name);
    }
}

var person1 = new Person("Nicholas", 29, "software Engineer");
var person2 = new Person("Greg", 27, "Doctor");

person1.friends.push("Van");

alert(person1.friends); //"Shelby,Court,Van"
alert(person2.friends); //"Shelby,Court"
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true

//Dynamic prototype mode
function Person( name, age, job ) {
    //Attributes
    this.name = name;
    this.age = age;
    this.job = job;

    //method
    if( typeof this.sayName != "function" ) {
        Person.prototype.sayName = function() {
            alert(this.name);
        }
    }
}

var person = new Person("Nicholas", 29, "software Engineer");
person.sayName();

//Parasitic constructor mode
function SpecialArray() {
    //create array
    var values = new Array();

    //add value
    values.push.apply(values,arguments);

    //add new method
    values.toPipedString = function(){
        return this.join("|");
    };

    //return array
    return values;
}

var colors = new SpecialArray("red","blue","green");
alert(colors.toPipedString()); //"red|blue|green"

// Safe constructor pattern
function Person( name, age, job ) {
    //create the object to return
    var o = new Object();

    //You can define private variables and functions here

    //add method
    o.sayName = function() {
        alert(name);
    };

    //return object
    return o;
}

var person = Person("Nicholas", 29, "software Engineer");
Person.sayName(); //"Nicholas"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859360&siteId=291194637