これを解決するために最善を尽くしましたが、行き詰まってしまいました。4 番目のアラートが未定義を返すのはなぜですか?
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