一、使用new操做符後跟Object構造函數 如: 函數
var person = new Object(); 能夠寫成 var person = {}; this
person.name = "kitty"; 對象
person.age = 25; it
二、使用「對象字面量」表示法,如: io
var person = { function
name : "kitty", test
age:25 構造函數
}; 方法
三、工廠模式 方法: new
var createPerson = function(name,age,job){
var person = new Object();
person.name = name;
person.age = age;
person.job = job;
person.sayName = function(){
alert(person.name);
}
return person;
}
var person1 = createPerson("zh","62","Doctor");
person1.sayName();
四、構造函數模式 方法
function Person(name,age,job){ this.name = name; this.age = age; this.job = job; this.sayName = function(){ alert(this.name); }; }; var person1 = new Person("zhou",23,"test"); person1.sayName();