1.工廠模式
function Person(name,age,job){
//創(chuàng)建對象
var o = {};
o.name = name;
o.age = age;
o.job = job;
o.showName = function(){
alert(this.name);
};
.showName = function(){
alert(this.age);
};
.showName = function(){
alert(this.age)
};
return o;
}
var Tom = Person('tom',18,'程序員');
Tom.showName();
2.構造函數(shù)
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.showName = function(){
alert(this.name);
};
this .showAge = function(){
alert(this.age);
};
this.showJob = function(){
alert(this.age)
};
return o;
}
var Bob = new Person('bob',18,'產品');
Bob.showName();
3.原型模式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
}
//prototype原型
person.peototype.showName = function(){
alert(this.name);
};
person.peototype.showAge = function(){
alert(this.age);
};
person.peototype.showJob = function(){
alert(this.job);
};
var Lucy = new person('Lucy',19,'測試鼠');
alert(luck.showName());