使用带有自定义对象作为属性的jQuery.extend时出现意外行为
Unexpected behavior using jQuery.extend with custom objects as attributes
我正在使用jQuery(1.11.2)和$.extend来克隆对象。这并不像预期的那样奏效。
我有一个源对象,它包含两个属性,并将函数的自定义实例(新函数…)作为值。这些值似乎被复制为引用,而我希望它们被复制为值。请参阅下面的代码。
var AnyObj = function(){
this.attribute = "anyAttribute";
};
var target = {
a: new AnyObj(),
b: new AnyObj()
}
//this prints "anyAttribute"
console.log(target.a)
//now I create a deep copy
var copy = $.extend(true, {},target);
//and I change the attribute of the COPY(!!!)
copy.a.attribute = "YAYA";
//this prints an object with the value YAYA
console.log(copy.a)
//this should NOT print an object with the value YAYA
console.log(target.a)
这似乎是一个jQuery extend()错误。有关更多详细信息,请参阅以下内容:http://bugs.jquery.com/ticket/10014
相关文章:
- 使用带有自定义对象作为属性的jQuery.extend时出现意外行为
- jQuery extend 函数是怎么回事
- 使用 YUI 3 跟踪 jQuery.extend
- 我什么时候应该在jQuery中使用$.fn.extend()
- JQuery .extend() and Javascript class
- jQuery.extend 抛出带有自引用的堆栈溢出异常
- jQuery.extend() deep clone nested objects
- jQuery ui Datepicker - global extend/override?
- jquery extend for i18n
- 使用 fn.extend() 和 Google 闭包编译器扩展 jQuery
- 防止jQuery.fn.extend多次触发
- jQuery$.extend名称空间
- 在使用jquery$.extend()时忽略属性
- jQuery$.extend,而不覆盖目标属性
- 理解jQuery$.extend()的正确用法
- 使用$extend()为jQuery中的switch语句函数添加更多事例
- 为什么jQuery.extend()比Lodash.clone()快
- 在jQuery.extend()中使用$(this)
- jQuery: object[property].push()在使用jQuery.extend()创建新对象时修改旧对象
- jquery.Extend (true, [], obj)不创建深度拷贝