JS CLONE使用法
js 代码:
a=5;
b=a;
b=4;
console.log(a,b);
结果输出:
5 4
而:
a={}
b=a
b.title=”title”
console.log(a,b);
If you use an = statement to assign a value to a var with an object on the right side, javascript will not copy but reference the object
对象的赋值,是传递的引使用,假如想要传值,则能用jquery:
$.extend({}, obj);
例子2:
a = {
'title': 'title',
'child': {
'info': {
'title': 'child title',
}
}
}
b = $.extend({}, a.child);
b.info.title=”new title”;
console.log(a.child.info.title, b.info.title);
结果输出:new title new title
这是由于:
The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.
第一个参数传true,进行 deepcopy,即对递归copy,对象的值也被copy了。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » JS CLONE使用法