我得到了一个带有全局变量和方法的JavaScript类,并且一个specefic方法使得一些引用。Javascript类的全局变量值丢失
DOOM = (function() {
/** Class VALIDATIONS **/
var validation = function (form, inputDiv, action) {
this.form = form;
this.inputDiv = inputDiv; // -- This Variable
this.action = action; // -- And This Variable
};
validation.prototype = {
validate: function (rules, messages) {
this.rules = rules;
this.messages = messages;
console.log(inputDiv); // -- here still have value
console.log(inputDiv);
$(this.form).validate({
rules: this.rules,
messages: this.messages,
submitHandler: function(form){
var getDom = new DOOM.DOM();
var data = getDom.buildJSONValuesByJSON(getDom.buildJSONObjByForm(inputDiv)); // -- But here, already haven't value
var sendData = new DOOM.callAJAX(action);
sendData.start(data);
console.log('[submitHandler] = '+ data);
return false;
},
作为解决方法,我必须在全局变量仍有价值的地方分配值。但我的问题是,为什么这些变量失去了他的价值?
inputDiv'的'第二外观是一个不同的功能,内侧[其具有不同的范围。](http://stackoverflow.com/questions/500431/what-is-the-scope-of-变量在JavaScript中) – Blazemonger
'inputDiV'和'action'不是全局变量,它们是'validation'函数中的局部变量。 – Barmar
你应该使用'this.inputDiv'。 – Barmar