我有一个 javascript 对象,该对象具有一个属性,该属性在对象被实例化后分配了一个值。然后我想在对象的函数中使用该值。然而,该函数不是新分配的值,而是只看到属性的初始值(即 null)
var _protocolData = new function () {
var test = null;
var getTest = function () {
return test;
};
return {
Test: test,
GetTest: getTest
};
};
//
// assign the new property value
_protocolData.Test = "New Value";
//
// I expect the dialog box to be populated with "New Value".
alert(_protocolData.GetTest()); // alert box is empty (null)