方法一:对象工厂
function CreatePerson(name, age, gender) {
var obj = new Object();
obj.name = name;
obj.age = age;
obj.gender = gender;
return obj;
}
var 孙悟空 = CreatePerson("孙悟空", 100, "男");
var 猪八戒 = CreatePerson("猪八戒", 9000, "男");
console.log(孙悟空);
console.log(猪八戒);
方法二:类方法(构造函数方法)
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
var 孙悟空 = new Person("孙悟空", 1000, "男");
var 猪八戒 = new Person("猪八戒", 900, "男");
var 白骨精 = new Person("白骨精", 1000, "女");
console.log(孙悟空);
console.log(猪八戒);
console.log(白骨精);