栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

nodeJS的深度扩展(如jQuery)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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, clone, 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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号