我得到了一個帶有全局變量和方法的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