我得到了这个示例代码,我尝试将值分配给单例的对象:
var myVariable;
var mySingleton = ( function() {
var INSTANCE;
var single = {
variable1 : null,
variable2 : null,
variable3 : null,
variableOptions : [ {
variableOption1 : null,
variableOption2 : null
} ]
};
I meant like that:
return {
getMySingleton : function() { return single },
setMySingleton : function( singletonParameter ) { $.extend( single, singletonParameter ) },
getSingletonOptions : function ( index ) { return single.variableOptions[index] },
setSingletonOptions : function( index, OptionsSingleton ) { $.extend( single.variableOptions[index] , OptionsSingleton ) },
getInstance : function() {
if (!INSTANCE) {
INSTANCE = this;
}
return INSTANCE;
}
}
})();
$(function() {
var idx = 0;
myVariable = mySingleton.getInstance(); // new empty instance
$.each( $('#list > li'), function() {
myVariable.setSingletonOptions(idx, {
variableOption1 : $(this).find('[id*="variableOption1_"]').val(),
variableOption2 : $(this).find('[id*="variableOption2_"]').val()
});
idx++;
});
myVariable.setMySingleton({
variable1 : $(this).find('[id*="variable1_"]').val(),
variable2 : $(this).find('[id*="variable2_"]').val(),
variable2 : $(this).find('[id*="variable2_"]').val()
});
console.log( myVariable.getMySingleton() );
});
但是当我调试到console.log时,我将变量内容变为null ...我错过了什么?我认为这是关于价值传递......
干杯, 路易