javascript 设计模式 笔记

继承模式
function EditInPlaceArea(id, parent, value) {
EditInPlaceArea.superclass.constructor.call(this, id, parent, value);
};
extend(EditInPlaceArea, EditInPlaceField);
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
}
subClass 子类中重新定义prototype中的熟悉 就可以获得类似extend的效果、、、还是有点模糊,不理解
 
原型继承
var Person = {
name: ‘default name’,
getName: function() {
return this.name;
}
};
var reader = clone(Person);
alert(reader.getName()); // This will output ‘default name’.
reader.name = ‘John Smith’;
alert(reader.getName()); // This will now output ‘John Smith’.
function clone(object) {
function F() {}
F.prototype = object;
return new F;
}
利用clone
 
掺元类继承
/* Augment function. */
function augment(receivingClass, givingClass) {
for(methodName in givingClass.prototype) {
if(!receivingClass.prototype[methodName]) {
receivingClass.prototype[methodName] = givingClass.prototype[methodName];
}
}
}
/* Augment function, improved. */
function augment(receivingClass, givingClass) {
if(arguments[2]) { // Only give certain methods.
for(var i = 2, len = arguments.length; i < len; i++) {
receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]];
}
}
else { // Give all methods.
for(methodName in givingClass.prototype) {
if(!receivingClass.prototype[methodName]) {
receivingClass.prototype[methodName] = givingClass.prototype[methodName];
}
}
}
}

发表评论