1.工厂模式:及时由一个工厂类根据传入的参数决定生产那种产品类
function createPerson(name,age,job)
{
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function()
{
alert(this.name);
}
return o;
}
var person = createPerson("xiaoqiang",12,"student");
person.sayName();
2.构造函数模式:创建自定义的构造函数,从而定义自定义对象类型的属性和方法
function Person(name,age)
{
this.name = name;
this.sayName = function()
{
alert(this.name);
}
}
var p = new Person("xiaoqiang",22);
p.sayName();
3.原型模式:不必在构造函数中定义对象实例的信息,而是将信息直接添加到原型对象中
function Person(){};
Person.prototype =
{
name : "xiao",
sayName : function(){alert(this.name)}
};
var p = new Person();
p.sayName();