iframe1.map_data=iframe2.map_data
这不是将iframe1.map_data
设置为对iframe2.map_data
的引用吗?
当新文件加载到iframe2
中时,为什么iframe1.map_data
仍然包含数据?
最佳答案
我们不要让 iframe 对此感到困惑。
var obj1 = {
a: {test: 123};
}
var obj2 = {};
obj2.a = obj1.a;
// both properties point to the same object.
obj1.a; // {test:123}
obj2.a; // {test:123}
// modify a property of obj1.
obj1.a = 'abc';
// properties point to different objects.
obj1.a; // 'abc'
obj2.a; // {test:123}
这基本上就是你所说的,没有 iframe。
因此属性指向对象,而不是其他属性。 obj1.a = obj2.a
不以任何方式链接属性。它只是将两个属性设置为指向同一个对象。
如果我稍后将这些属性之一指向不同的对象,它不会更改任何其他属性。
<小时/>但是,如果修改多个属性指向的对象,则可以传播有意义的更改。
var obj1 = {
a: {test: 123};
}
var obj2 = {};
obj2.a = obj1.a;
// modify a property of the shared object.
obj1.a.test = 456;
// both properties point to the same object.
obj1.a; // {test:456}
obj2.a; // {test:456}
为什么这会改变两者?因为在本例中,obj1.a
和 obj2.a
都引用同一个对象,并且该对象已更改。
这次我们修改共享对象。之前,我们修改了未共享的对象。
看出区别了吗?
关于javascript - 为什么引用对象消失后变量仍然包含数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14226380/