深度扩展(像jQuery的)为nodeJS

前端之家收集整理的这篇文章主要介绍了深度扩展(像jQuery的)为nodeJS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力与nodeJS中的对象的深层副本。我自己的延伸是废话。下划线的延伸是平的。在stackexchange上有相当简单的扩展变体,但没有一个甚至接近jQuery.extend(true,{},obj,obj,obj)。
(大多数实际上是可怕的和拧紧asnyc代码的好处。)

因此,我的问题:NodeJS有一个很好的副本吗?有任何人移植jQuery的?

解决方法

你想要jQuery的,所以只是使用它:
function extend() {
    var options,name,src,copy,copyIsArray,clone,target = arguments[0] || {},i = 1,length = arguments.length,deep = false,toString = Object.prototype.toString,hasOwn = Object.prototype.hasOwnProperty,push = Array.prototype.push,slice = Array.prototype.slice,trim = String.prototype.trim,indexOf = Array.prototype.indexOf,class2type = {
          "[object Boolean]": "boolean","[object Number]": "number","[object String]": "string","[object Function]": "function","[object Array]": "array","[object Date]": "date","[object RegExp]": "regexp","[object Object]": "object"
        },jQuery = {
          isFunction: function (obj) {
            return jQuery.type(obj) === "function"
          },isArray: Array.isArray ||
          function (obj) {
            return jQuery.type(obj) === "array"
          },isWindow: function (obj) {
            return obj != null && obj == obj.window
          },isNumeric: function (obj) {
            return !isNaN(parseFloat(obj)) && isFinite(obj)
          },type: function (obj) {
            return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"
          },isPlainObject: function (obj) {
            if (!obj || jQuery.type(obj) !== "object" || obj.nodeType) {
              return false
            }
            try {
              if (obj.constructor && !hasOwn.call(obj,"constructor") && !hasOwn.call(obj.constructor.prototype,"isPrototypeOf")) {
                return false
              }
            } catch (e) {
              return false
            }
            var key;
            for (key in obj) {}
            return key === undefined || hasOwn.call(obj,key)
          }
        };
      if (typeof target === "boolean") {
        deep = target;
        target = arguments[1] || {};
        i = 2;
      }
      if (typeof target !== "object" && !jQuery.isFunction(target)) {
        target = {}
      }
      if (length === i) {
        target = this;
        --i;
      }
      for (i; i < length; i++) {
        if ((options = arguments[i]) != null) {
          for (name in options) {
            src = target[name];
            copy = options[name];
            if (target === copy) {
              continue
            }
            if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
              if (copyIsArray) {
                copyIsArray = false;
                clone = src && jQuery.isArray(src) ? src : []
              } else {
                clone = src && jQuery.isPlainObject(src) ? src : {};
              }
              // WARNING: RECURSION
              target[name] = extend(deep,copy);
            } else if (copy !== undefined) {
              target[name] = copy;
            }
          }
        }
      }
      return target;
    }

和一个小测试,以显示它做深拷贝

extend(true,{
        "name": "value"
    },{
        "object": "value","other": "thing","inception": {
            "deeper": "deeper","inception": {
                "deeper": "deeper","inception": {
                    "deeper": "deeper"
                }
            }
        }
    }
)

但请记住提供归因:https://github.com/jquery/jquery/blob/master/src/core.js

猜你在找的jQuery相关文章