标签:
JavaScript继承有两种较好的方法
1. prototype继承
YUI的做法如下:
function extend(Child,Parent){
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.contructor = Child;
Child.uber = Parent.prototype;
}
2. 属性复制继承(深拷贝)
jQuery的做法如下:
functon deepCopy(p,c){
var c = c || {};
for(var i in p){
if(typeof p[i] === ‘object‘){
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(p[i],c[i]);
}
else{
c[i] = p[i];
}
}
return c;
}
使用 var child = deepCopy(parent);
参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html
标签:
原文地址:http://www.cnblogs.com/mengff/p/5005597.html