创建对象

1. 原始方法

var person = new Object();
person.name = "Andy";
person.age = "17";
person.sayName = function() {
    console.log(this.name);
};
person.sayName(); // Andy
var person = {
    name: "Andy",
    age: "17",
    sayName: function() {
        console.log(this.name);
    }
};
person.sayName(); // Andy

2. 工厂方法

function createPerson() {
    var person = new Object();
    person.name = "Andy";
    person.age = "17";
    person.sayName = function() {
        console.log(this.name);
    }
    return person;
}
var person = createPerson();
person.sayName(); // Andy
function createPerson(name, age) {
    var person = new Object();
    person.name = name;
    person.age = age;
    person.sayName = sayName;
    return person;
}
function sayName() {
    console.log(this.name);
}
var person1 = createPerson("Andy", "17");
person1.sayName(); // Andy
var person2 = createPerson("Lucy", "16");
person2.sayName(); // Lucy

3. 构造函数

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.sayName = function() {
        console.log(this.name);
    }
}
var person = new Person("Andy", "17");
person.sayName(); // Andy

4. 原型函数

function Person() {}
Person.prototype.name = "Andy";
Person.prototype.age = "17";
Person.prototype.friends = new Array("Jack", "Tony");
Person.prototype.sayName = function() {
    console.log(this.name);
}
Person.prototype.showFriends = function() {
    console.log(this.friends);
}
var person1 = new Person();
var person2 = new Person();
person1.sayName(); // Andy
person2.sayName(); // Andy
person1.name = "Lucy";
person1.sayName(); // Lucy
person2.sayName(); // Andy
person1.showFriends(); //  Jack, Tony
person2.showFriends(); // Jack, Tony
person1.friends.push("Tom");
person1.showFriends(); // Jack, Tony, Tom
person2.showFriends(); // Jack, Tony, Tom

5. 混合构造函数\原型函数

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.friends = new Array("Jack", "Tony");
}
Person.prototype.sayName = function() {
    console.log(this.name);
}
Person.prototype.showFriends = function() {
    console.log(this.friends);
}
var person1 = new Person("Andy", "17");
var person2 = new Person("Lucy", "16");
person1.sayName(); // Andy
person2.sayName(); // Lucy
person1.showFriends(); // Jack, Tony
person2.showFriends(); // Jack, Tony
person1.friends.push("Tom");
person1.showFriends(); // Jack, Tony, Tom
person2.showFriends(); // Jack, Tony

6. 动态原型方法

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.friends = new Array("Jack", "Tony");
    if(typeof Person._initialized == "undefined"){
        Person.prototype.sayName = function() {
            console.log(this.name);
        }
        Person.prototype.showFriends = function() {
            console.log(this.friends);
        }
        Person._initialized = true;
    }else{}
}
var person1 = new Person("Andy", "17");
var person2 = new Person("Lucy", "16");
person1.sayName(); // Andy
person2.sayName(); // Lucy
person1.showFriends(); // Jack, Tony
person2.showFriends(); // Jack, Tony
person1.friends.push("Tom");
person1.showFriends(); // Jack, Tony, Tom
person2.showFriends();// Jack, Tony

你可能感兴趣的:(创建对象)