function showSalary(){
alert(this.salary);
}
Factory methods
First create the object, and then add properties and methods, do not need to use NEW to create objects,
If the function written in-house, each call will create a new function, on the outside packaging of bad
function createWorker(sAage,sSalary,sDepartment){
var oWorker=new Object();
oWorker.age=sAage;
oWorker.salary=sSalary;
oWorker.department=sDepartment;
//创建函数的方式一,指向我们文章开头写好的函数名,缺点是函数在对象的外部,封装性不好
oWorker.tellSalary=showSalary;
//创建函数的方式二,在对象内部声明匿名函数,虽然封装在对象内部了,但没个对象会有不同的函数版本,浪费内存
oWorker.tellSalary=function(){
alert(this.salary);
}
return oWorker;
}
var worker1=createWorker(24,1000,"Dev");
worker1.tellSalary();
var worker2=createWorker(24,3000,"Dev");
worker2.tellSalary();
Constructor methods
In the constructor does not create an internal object, use this keyword, when used to create new operator, if the function declaration in the internal, and plant the same way as the existence of the problem, create a function to repeat. Each object is a function of the creation of an independent version. On the outside packaging of bad
function Worker(sAage,sSalary,sDepartment){
this.age=sAage;
this.salary=sSalary;
this.department=sDepartment;
//同工厂方式
this.tellSalary=showSalary;
//同工厂方式
this.tellSalary=function(){
alert(this.salary);
}
}
var worker3=new Worker(24,1000,"Dev");
worker3.tellSalary();
var worker4=new Worker(24,3000,"Dev");
worker4.tellSalary();
Prototype approach to create the object can not be used when passing parameters structure must first create the object, and then change the value of property
function Worker(){
}
Worker.prototype.age=24;
Worker.prototype.salary=1000;
Worker.prototype.department="Dev";
Worker.prototype.homeaddress=new Array("www","dd");
// 创建多个对象时,要想一想此处为什么不会和构造函数方式及工厂方式一样,创建多个函数的实例
//要理解prototype的概念,prototype是父对象的一个实例
Worker.prototype.tellSalary=function(){
alert(this.age);
}
var worker5=new Worker();
var worker6=new Worker();
worker5.tellSalary();
alert(worker5.homeaddress)
//修改原型中引用类型的值,会影响到所有已经实例化的对象
worker6.homeaddress.push("gg")
alert(worker5.homeaddress)
Hybrid prototype and constructor methods
Transmission parameters can be constructed objects, multiple instances of a function object to share
function Worker(sAage,sSalary,sDepartment){
this.age=sAage;
this.salary=sSalary;
this.department=sDepartment;
this.homeaddress=new Array("www","dd");
}
//只创建tellSalary函数一个实例,没有内存浪费
Worker.prototype.tellSalary=function(){
alert(this.age);
}
var worker7=new Worker(23,3000,"Dev");
var worker8=new Worker(43,1000,"Dev");
worker7.tellSalary();
alert(worker7.homeaddress) // ww dd
worker8.homeaddress.push("gg")
alert(worker7.homeaddress) //www dd
alert(worker8.homeaddress) // www dd gg
The fourth way is also very scattered look, you can improve the look
var person=function(sname,sage){
this.name=sname;
this.age=sage;
};
person.prototype={
tellName:function(){
alert(this.name);
},
tellAge:function(){
alert(this.age);
}
};
Advantages:
1. Communication parameters can construct a new object
2. When there are multiple targets generated will not create more than one function entity, there is no memory waste
3. Package like a good separation of properties and methods,







