【发布时间】:2016-03-03 19:12:43
【问题描述】:
我有一个带有全局变量和方法的 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的第二次出现在另一个函数中,which has a different scope. -
inputDiV和action不是全局变量,它们是validation函数中的局部变量。 -
你应该使用
this.inputDiv。 -
@Blazemonger 那么,我不能使用那个变量吗?因为范围不允许
-
@Barmar 是的,我就是这样做的
标签: javascript class scope