[英]JavaScript object behavior
I do not get stumped often but I am just at a loss of words right now. 我不会经常感到难过,但是现在我只是一言不发。
Using Chrome 37.0.2062.120 m 使用Chrome 37.0.2062.120 m
Can someone explain to me why my console.log();
有人可以向我解释为什么我的console.log();
is reporting these results? 正在报告这些结果?
JS code JS代码
var some_obj = {min:1};
var another_obj = {};
console.log(some_obj);
another_obj['sometarget'] = some_obj; // <- What is this sorcery?!?!?
another_obj['sometarget']['required'] = true;
console.log(some_obj);
console.log() output console.log()输出
Object {min: 1}
Object {min: 1, required: true}
JSFIDDLE 的jsfiddle
http://jsfiddle.net/qrnaw7j2/1/ http://jsfiddle.net/qrnaw7j2/1/
Objects in javascript are shared via reference. javascript中的对象通过引用共享。
So some_obj
and another_obj['sometarget']
point to the same place in memory. 因此some_obj
和another_obj['sometarget']
指向内存中的同一位置。
When you set ['required'] = true;
当您设置['required'] = true;
in any of them, they will both get updated. 在任何一个中,它们都将被更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.