From 5f9bc7aed6ec0d9e6d44a964d57c4c3814ac1a34 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 4 Sep 2014 03:44:39 -0400 Subject: [PATCH] refactor(util) extend --- lib/util.js | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/util.js b/lib/util.js index bcf9f271..c591cb59 100644 --- a/lib/util.js +++ b/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;