[英]JavaScript object setting values to object but not updating to firebase
[英]Setting object values in JavaScript
我已盡力解決這個問題,現在我被卡住了,為什么第四個警報返回未定義?
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
}
解析度:
setInFunctionCalledFromOutside : _setInFunctionCalledFromOutside, // Won't work
setInFunctionCalledFromOutsideGetter : function(){
return _setInFunctionCalledFromOutside; // Will work
}
...
alert(test.setInFunctionCalledFromOutside); // Broken, returns undefined
alert(test.setInFunctionCalledFromOutsideGetter()); // Now works
這個:
return {
basic : "Easy stuff",
setDirectlyInside : _setDirectlyInside,
setInFunctionCalledInside : _setInFunctionCalledInside,
functionCallFromOutside : function(){
_setInFunctionCalledFromOutside = "Why does this come back as undefined?";
},
setInFunctionCalledFromOutside : _setInFunctionCalledFromOutside
}
......不會造成setInFunctionCalledFromOutside
總是返回相同的值_setInFunctionCalledFromOutside
。 相反, _setInFunctionCalledFromOutside
將在return
語句執行時進行計算,其值將放在setInFunctionCalledFromOutside
。 因此, functionCallFromOutside()
對setInFunctionCalledFromOutside
沒有影響。
@nnnnnn 說得最好 ,但他刪除了他的回答。 setInFunctionCalledFromOutside
不是指向 _setInFunctionCalledFromOutside
的指針 。 在創建返回的對象時,為setInFunctionCalledFromOutside
分配了_setInFunctionCalledFromOutside
具有的值。 由於尚未分配任何值,因此值undefined
。 更改一個的值,不會更改另一個的值。
一個更簡單的例子說明了同樣的原則:
var i = 0;
var n = i;
i++;
alert(i); // 1
alert(n); // still 0
要添加到其他人 - 供參考,JS按值傳遞字符串和數字。 它通過引用傳遞/分配對象,數組和函數。
這不是你要擊中的障礙(因為你的內部變量在你將它分配給你的外部變量時是undefined
的),但即使你確實修復了第一部分:
var internal = "string";
return {
external : internal,
setInternal : function (val) { internal = val; }
};
這仍然無法解決問題,因為obj.external
將永遠是“字符串”。
為什么?
因為在賦值時將external
設置為與internal
相同的值,並且不等於internal
的reference
(即:指向它的指針)。
如果您正在尋找指針,那么將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"
它為什么現在有用?
因為internal是一個對象,當你分配external
的值時,你給它一個指向internal
也有一個指針的對象的指針。
當你更改internal
的屬性時,比如internal.string
,因為external
指向同一個對象, external.string
具有相同的值。
如果你將internal
設置為完全不同的東西,那么你將internal
指向別的東西(比如internal = null;
),因此當你修改internal
時, external
將不再改變,因為internal
不再指向它使用的對象成為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.