[英]Setting object values in JavaScript
I've tried my best to work this out, now I'm stuck, why does the fourth alert return undefined? 我已尽力解决这个问题,现在我被卡住了,为什么第四个警报返回未定义?
function buttonClick()
{
var myTest = function()
{
var _setDirectlyInside = "So far so good...";
var _setInFunctionCalledInside;
var _setInFunctionCalledFromOutside;
(function(){
_setInFunctionCalledInside = "This would indicate scope isn't the problem?";
})();
return {
basic : "Easy stuff",
setDirectlyInside : _setDirectlyInside,
setInFunctionCalledInside : _setInFunctionCalledInside,
functionCallFromOutside : function(){
_setInFunctionCalledFromOutside = "Why does this come back as undefined?";
},
setInFunctionCalledFromOutside : _setInFunctionCalledFromOutside
}
};
var test = myTest();
alert(test.basic); // Returns "Easy stuff"
alert(test.setDirectlyInside); // Returns "So far so good..."
alert(test.setInFunctionCalledInside); // Returns "This would indicate scope isn't the problem?"
test.functionCallFromOutside();
alert(test.setInFunctionCalledFromOutside); // Broken, returns undefined
}
Resolution: 解析度:
setInFunctionCalledFromOutside : _setInFunctionCalledFromOutside, // Won't work
setInFunctionCalledFromOutsideGetter : function(){
return _setInFunctionCalledFromOutside; // Will work
}
...
alert(test.setInFunctionCalledFromOutside); // Broken, returns undefined
alert(test.setInFunctionCalledFromOutsideGetter()); // Now works
This: 这个:
return {
basic : "Easy stuff",
setDirectlyInside : _setDirectlyInside,
setInFunctionCalledInside : _setInFunctionCalledInside,
functionCallFromOutside : function(){
_setInFunctionCalledFromOutside = "Why does this come back as undefined?";
},
setInFunctionCalledFromOutside : _setInFunctionCalledFromOutside
}
...will not cause setInFunctionCalledFromOutside
to always return the same value of _setInFunctionCalledFromOutside
. ......不会造成setInFunctionCalledFromOutside
总是返回相同的值_setInFunctionCalledFromOutside
。 Rather, _setInFunctionCalledFromOutside
will be evaluated at the time the return
statement executes, and its value will be placed in setInFunctionCalledFromOutside
. 相反, _setInFunctionCalledFromOutside
将在return
语句执行时进行计算,其值将放在setInFunctionCalledFromOutside
。 Thus, functionCallFromOutside()
will have no effect on setInFunctionCalledFromOutside
. 因此, functionCallFromOutside()
对setInFunctionCalledFromOutside
没有影响。
@nnnnnn said it best , but he deleted his answer. @nnnnnn 说得最好 ,但他删除了他的回答。 setInFunctionCalledFromOutside
is not a pointer to _setInFunctionCalledFromOutside
. setInFunctionCalledFromOutside
不是指向 _setInFunctionCalledFromOutside
的指针 。 At the time the returned object was created, setInFunctionCalledFromOutside
was assigned the value that _setInFunctionCalledFromOutside
had. 在创建返回的对象时,为setInFunctionCalledFromOutside
分配了_setInFunctionCalledFromOutside
具有的值。 Since, no value had yet been assigned, the value was undefined
. 由于尚未分配任何值,因此值undefined
。 Changing the value of the one, doesn't change the value of the other. 更改一个的值,不会更改另一个的值。
A simpler example that illustrates the same principle: 一个更简单的例子说明了同样的原则:
var i = 0;
var n = i;
i++;
alert(i); // 1
alert(n); // still 0
To add to the others - for reference, JS passes strings and numbers by value. 要添加到其他人 - 供参考,JS按值传递字符串和数字。 It passes/assigns objects, arrays and functions by reference. 它通过引用传递/分配对象,数组和函数。
This isn't quite the snag that you're hitting (because your internal variable is undefined
by the time you assign it to your external variable), but even if you did get the first part fixed: 这不是你要击中的障碍(因为你的内部变量在你将它分配给你的外部变量时是undefined
的),但即使你确实修复了第一部分:
var internal = "string";
return {
external : internal,
setInternal : function (val) { internal = val; }
};
That still doesn't fix the problem, as obj.external
will always continue to be "string". 这仍然无法解决问题,因为obj.external
将永远是“字符串”。
Why? 为什么?
Because you set external
to be the same value as internal
at the time of assignment, and not to be equal to a reference
of internal
(ie: a pointer to it). 因为在赋值时将external
设置为与internal
相同的值,并且不等于internal
的reference
(即:指向它的指针)。
If you're looking for a pointer, then make internal
an object: 如果您正在寻找指针,那么将internal
一个对象:
var internal = { string : "my string" };
return {
external : internal,
setInternal : function (val) { internal.string = val; }
};
obj.external.string; // "my string"
obj.setInternal("Bob was here");
obj.external.string; // "bob was here"
Why does it now work? 它为什么现在有用?
Because internal is an object, and when you assign external
's value, you give it a pointer to the object which internal
also has a pointer to. 因为internal是一个对象,当你分配external
的值时,你给它一个指向internal
也有一个指针的对象的指针。
When you change a property of internal
, like internal.string
, because external
points to the same object, external.string
has the same value. 当你更改internal
的属性时,比如internal.string
,因为external
指向同一个对象, external.string
具有相同的值。
If you set internal
to something completely different, then you're pointing internal
at something else (like internal = null;
), therefore external
will no longer change, when you modify internal
, because internal
is no longer pointing at the object which it used to be. 如果你将internal
设置为完全不同的东西,那么你将internal
指向别的东西(比如internal = null;
),因此当你修改internal
时, external
将不再改变,因为internal
不再指向它使用的对象成为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.