[英]Global variables values on Javascript Class are lost
I got a javascript class with global variables and methods and a specefic method makes some extrange. 我有一个带有全局变量和方法的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;
},
As workaround, I had to assign value where still the global variables has value. 解决方法是,我必须在仍然具有全局变量值的地方分配值。 But my question is, Why those variables lost his values?.
但是我的问题是,为什么这些变量失去了他的价值?
Variables named in the parameter list of a function are always local variables, not global variables. 在函数的参数列表中命名的变量始终是局部变量,而不是全局变量。 So in the
validation()
constructor function, inputDiv
is a local variable. 因此,在
validation()
构造函数中, inputDiv
是局部变量。 If you're seeing something when you do console.log(inputDiv)
in validation.prototype.validate()
, it must be from a different variable declared outside the code that you posted. 如果你看到的东西,当你做
console.log(inputDiv)
在validation.prototype.validate()
它必须是从您发布的代码之外声明不同的变量。
To access the inputDiv
that was provided to the constructor in the methods, you need to use this.inputDiv
. 要访问
inputDiv
中提供给构造函数的inputDiv
,需要使用this.inputDiv
。
validation.prototype = {
validate: function (rules, messages) {
this.rules = rules;
this.messages = messages;
var inputDiv = this.inputDiv;
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));
var sendData = new DOOM.callAJAX(action);
sendData.start(data);
console.log('[submitHandler] = '+ data);
return false;
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.