mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 18:18:56 +00:00
refactor(util) extend
This commit is contained in:
parent
cd91a3efa3
commit
5f9bc7aed6
1 changed files with 26 additions and 15 deletions
41
lib/util.js
41
lib/util.js
|
|
@ -185,27 +185,38 @@
|
|||
};
|
||||
|
||||
/**
|
||||
* copy pObj properties to pTargetObject
|
||||
* copy objFrom properties to target
|
||||
*
|
||||
* @pTarget
|
||||
* @pObj
|
||||
* @target
|
||||
* @objFrom
|
||||
*/
|
||||
this.extend = function(pTarget, PObj) {
|
||||
var i, n, lObj,
|
||||
isFunc = Util.isFunction(PObj),
|
||||
isArray = Util.isArray(PObj),
|
||||
isObj = Util.isObject(pTarget),
|
||||
ret = isObj ? pTarget : {};
|
||||
this.extend = function(target, objFrom) {
|
||||
var obj,
|
||||
keys,
|
||||
proto,
|
||||
isFunc = Util.isFunction(objFrom),
|
||||
isArray = Util.isArray(objFrom),
|
||||
isObj = Util.isObject(target),
|
||||
ret = isObj ? target : {};
|
||||
|
||||
if (isArray)
|
||||
for (i = 0, n = PObj.length; i < n; i++)
|
||||
ret = Util.extend(pTarget, PObj[i]);
|
||||
objFrom.forEach(function(item) {
|
||||
ret = Util.extend(target, item);
|
||||
});
|
||||
|
||||
|
||||
else if (PObj) {
|
||||
lObj = isFunc ? new PObj() : PObj;
|
||||
else if (objFrom) {
|
||||
obj = isFunc ? new objFrom() : objFrom;
|
||||
keys = Object.keys(obj);
|
||||
|
||||
for(i in lObj)
|
||||
ret[i] = lObj[i];
|
||||
if (!keys.length) {
|
||||
proto = Object.getPrototypeOf(objFrom);
|
||||
keys = Object.keys(proto);
|
||||
}
|
||||
|
||||
keys.forEach(function(name) {
|
||||
ret[name] = obj[name];
|
||||
});
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue