javascript面向对象的混入
作者:互联网
一、单混入(单继承)
function extend(sourceObj, targetObj) {
for (var key in targetObj) {
if (targetObj.hasOwnProperty(key)) {
sourceObj[key] = targetObj[key];
}
}
return sourceObj;
}
二、多继承
function mix() {
var target = arguments[0];
for (var i = 1, len = arguments.length; i < len; i++) {
var arg = arguments[i];
for (var key in arg) {
if (arg.hasOwnProperty(key)) {
target[key] = arg[key];
}
}
}
return target;
}
标签:混入,target,javascript,面向对象,targetObj,key,arg,var,sourceObj 来源: https://blog.51cto.com/u_3409716/2904266