/*
* 工厂模式
*/
function createObj (name, age) {
var obj = new Object()
obj.name = name
obj.age = age
obj.run = function () {
return this.name + this.age + '运行中'
}
return obj
}
var box1 = createObj('xiaoming',24)
/*
* 构造函数模式
*/
function Box (name,age) {
this.name = 'haha'
this.age = 25
this.height = 123
this.run = function () {
return this.name + this.age + '运行中'
}
}
var box2 = new Box()
/*
* 字面量创建构造函数的原型对象
*/
function Obj(){}
Obj.prototype = {
constructor: Obj,
name: 'huchuan',
age: 23,
run: function () {
return this.name + this.age + '运行中'
}
}
var box3 = new Obj()
/*
* 组合构造函数模式+原型模式
*/
function MixObj(name, age) {
this.name = name
this.age = age
this.family = ['mom','dad','sister']
}
MixObj.prototype = {
constructor: MixObj,
run : function () {
return this.name + this.age + '运行中...'
}
}
var box4 = new MixObj('huchuan',24)
/*
将原型封装到构造函数
*/
function ProtoInFun (name,age) {
this.name = name
this.age = age
if(typeof this.run != 'function'){
ProtoInFun.prototype.run = function () {
return this.name + this.age + '运行中...'
}
}
}
ProtoInFun.prototype.run = function () {
return this.age + this.name + '运行中...'
}
var box6 = new ProtoInFun('wangli', 333)
function Grandpa () {
this.name = 'leon'
this.like = 'movie'
this.height = 123
}
function Dad () {
this.height = 158
}
function Son () {
this.run = function () {
alert('hello world')
}
}
Son.prototype.like = 'film'
var GrandSon = {
weight: '23kg'
}
Dad.prototype = new Grandpa()
Son.prototype = new Dad()
var son = new Son()
alert(son.weight)
console