伪类
- 一个函数对象被创建时,产生的函数对象会运行一个类似这样的代码
this.prototype = { constructor: this };
- 在我看来,这是原型的继承,类似于面向对象语言中类的继承
- 这是基于构造器调用模式实现的继承,即使用new,但是书中并不推荐使用new
- 一个原型继承的例子
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
var Mammal = function (name) {
this.name = name;
};
Mammal
.method('get_name', function () {
return this.name;
}).method('says', function () {
return this.saying || '';
});
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name();
console.log(name);
var Cat = function (name) {
this.name = name;
this.saying = 'meow';
};
Cat.prototype = new Mammal();
Cat.method('purr', function () {
return 'this is purr';
});
Cat.method('get_name', function () {
return this.says() + ' ' + this.name + ' ' + this.says();
});
var myCat = new Cat('Alex Xu');
var says = myCat.says();
var purr = myCat.purr();
var name = myCat.get_name();
console.log(says);
console.log(purr);
console.log(name);
Function.method('inherits', function (Parent) {
this.prototype = new Parent();
return this;
});
var Catt = function (name) {
this.name = name;
this.saying = 'catt';
}
.inherits(Mammal)
.method('purr', function () {
return 'this is purr';
})
.method('get_name', function () {
return this.says() + ' ' + this.name + ' ' + this.says();
});
var myCatt = new Catt('hhee');
console.log(myCatt.get_name());
console.log(myCatt.purr());
对象说明符
- 如果一个对象的构造函数有过多的参数,可以使用对象作为参数,方便阅读
var NewCat = function (params) {
this.name = params.name || 'None';
this.age = params.age;
this.weight = params.weight;
this.brithday = params.brithday;
return this;
};
var nc = new NewCat({
name: 'hhe',
age: 2,
weight: 3.3,
brithday: '1997'
});
console.log(nc);
原型
- 基于原型的继承方式,其实只是概念上的不同
- 书上称之为差异化继承
if (typeof Object.beget !== 'function') {
Object.create = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
}
var baseObject = {
name: 'base',
get_name: function () {
return this.name;
},
says: function () {
return this.saying || '';
}
};
var childObject = Object.create(baseObject);
childObject.name = 'child';
childObject.saying = 'heiheihei';
childObject.purr = function () {
console.log('this is prpr');
};
childObject.get_name = function () {
return this.says() + ' ' + this.name + ' ' + this.says();
};
console.log(childObject.get_name());
console.log(childObject.says());
函数化
- 上面提及的继承模式都没法保护隐私,所有对象可见。也无法得到私有变量和私有函数
- 利用函数化模式是更好的选择(回想一下闭包),函数化模式的实现步骤,以下步骤都在一个函数内
- 创建一个新对象
- 定义私有变量和方法,函数中通过var定义的普通变量
- 扩充方法,这些方法可以访问参数以及第二步中定义的私有变量
- 返回那个新对象
var baseObject = function (params) {
var that;
var girl = 'girl';
var that = {
'get_girl': function () {
return girl;
},
'says': function () {
return params.saying;
}
};
return that;
};
var obj = baseObject();
console.log(obj.girl);
console.log(obj.get_girl());