javascript 对象的复制
2016-04-18 10:32
573 查看
1. jQuery has a method that can be used to deep-clone objects, the$.extend() function. Let’s take a look at how it can be used:
注意:Pretty handy, eh? This method is a little slower than the JSON exploit, but that shouldn’t really be a problem when you’re only doing a few clones. If you’re doing hundreds or thousands of clones, it might be time to think about the JSON exploit or another solution.
2. A clever exploit of the JSON library to deep-clone objects
We can exploit the JSON library for a rather fast way of deep-cloning objects. Check it out:
3. tipJS中的一个方法,也不错 ,不过我自己还没完全理解这个方法的独到之处(或者说这个方法的用途)。
var bob = { name: "Bob", age: 32 }; var bill = $.extend(true, {}, bob); bill.name = "Bill"; console.log(bob); console.log(bill);
注意:Pretty handy, eh? This method is a little slower than the JSON exploit, but that shouldn’t really be a problem when you’re only doing a few clones. If you’re doing hundreds or thousands of clones, it might be time to think about the JSON exploit or another solution.
2. A clever exploit of the JSON library to deep-clone objects
We can exploit the JSON library for a rather fast way of deep-cloning objects. Check it out:
var bob = { name: "Bob", age: 32 }; var bill = (JSON.parse(JSON.stringify(bob))); bill.name = "Bill"; console.log(bob); console.log(bill);
3. tipJS中的一个方法,也不错 ,不过我自己还没完全理解这个方法的独到之处(或者说这个方法的用途)。
util__.cloneObject = function(obj, isFlat){ var newObj, k; if (obj == null || typeof obj != "object") return obj; if (!isFlat) { newObj = (obj instanceof Array) ? [] : {}; for (k in obj) { if (typeof obj[k] == "object") newObj[k] = util__.cloneObject(obj[k], false); else newObj[k] = obj[k]; } return newObj; } else return __cloneObjN(obj); }; var __cloneObjN = function(target) { if (util__.isFunction(Object.create)) __cloneObjN = function(o) { return Object.create(o); }; else { __cloneObjN = function(o) { function F() {}; F.prototype = o; return new F; }; } return __cloneObjN(target); };
相关文章推荐
- js中函数提升及var变量提示
- js 验证码 倒计时60秒
- js鼠标滚轮放大缩小图片
- js 格式化日期
- javascript !! 小技巧
- javaScript工厂模式分析
- JavaScript 技巧
- 三种方法实现js跨域访问
- JS中artdialog弹出框控件之提交表单思路详解
- js 获取当前服务器路径
- javascript 从定义到执行,你不知道的那些事
- js版A星算法
- Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享
- JS正则表达式将url转成json格式
- javascript:设计模式之单例模式
- json序列化
- js统计当前输入字数在指定的输入区域显示
- ExtJS的12种布局风格
- js 计算时间差
- arcgis for javascript ArcGISDynamicMapServiceLayer 过滤图层点