[英]Call javascript class method from another method
I have problem calling one method from another method in a Javascript class. 我在从Javascript类的另一个方法调用一个方法时遇到问题。
Here is the code 这是代码
function Spinner( max_value , min_value , div_id ){
var MAX_VALUE = 25;
var MIN_VALUE = -25;
var DEFAULT_VALUE = 0;
this.maxValue = max_value;
this.minValue = min_value;
this.divId = div_id;
this.spinnerControl; //spinner control object that is populated in getSpinner() method.
this.rotateSpinner = function (spinnerTextBoxId,up) {
document.getElementById(spinnerTextBoxId).value = up ? parseInt(document
.getElementById(spinnerTextBoxId).value) + 1 : parseInt(document
.getElementById(spinnerTextBoxId).value) - 1;
};
this.getSpinner = function( ){
spinnerControl = $('<input type="text"></input>')
.attr('id' , 'spinner')
.attr('val' , DEFAULT_VALUE)
.add( $('<ul></ul>')
.addClass("spinner")
.append(
$('<li></li>')
.append($('<input type="button"></input>')
.attr({
id : 'upButton',
value : '▲'
}).appendTo( $('<li></li>') )
)
).append( $('<li></li>')
.append($('<input type="button"></input>')
.attr({
id : 'downButton',
value : '▼'
}).appendTo( $('<li></li>') )
)
)
);
var timeoutId = 0;
$('#upButton' , spinnerControl).mousedown(function() {
console.log('mouse down');
timeoutId = setInterval(this.rotateSpinner('spinner' , true ) , 200);
}).bind('mouseup mouseleave', function() {
console.log('mouse left');
//clearTimeout(timeoutId);
});
// $('#downButton').mousedown(function() {
// alert('herer');
// //timeoutId = setInterval("rotateSpinner('spinner' , false ) ", 200);
// }).bind('mouseup mouseleave', function() {
// clearTimeout(timeoutId);
// });
return spinnerControl;
};
//class END
}
In this js file ,the error shows up in firebug : "Reference error : rotateSpinner" is not defined. 在此js文件中,错误显示在firebug中:未定义“参考错误:rotateSpinner”。
Why is it appearing ? 为什么会出现?
this
is referring the DOM-element, which in this case is the '#upButton'. this
是指DOM元素,在这种情况下为“ #upButton”。 If you add a variable that points to the class, you can then refer to this one instead of this
when defining functions within the method. 如果添加了指向该类的变量,则可以在方法中定义函数时引用该变量而不是
this
变量。
function Spinner( max_value , min_value , div_id ){
var MAX_VALUE = 25;
var MIN_VALUE = -25;
var DEFAULT_VALUE = 0;
this.maxValue = max_value;
this.minValue = min_value;
this.divId = div_id;
this.spinnerControl; //spinner control object that is populated in getSpinner() method.
this.rotateSpinner = function (spinnerTextBoxId,up) {
document.getElementById(spinnerTextBoxId).value = up ? parseInt(document
.getElementById(spinnerTextBoxId).value) + 1 : parseInt(document
.getElementById(spinnerTextBoxId).value) - 1;
};
this.getSpinner = function( ){
var self = this;
spinnerControl = $('<input type="text"></input>')
.attr('id' , 'spinner')
.attr('val' , DEFAULT_VALUE)
.add( $('<ul></ul>')
.addClass("spinner")
.append(
$('<li></li>')
.append($('<input type="button"></input>')
.attr({
id : 'upButton',
value : '▲'
}).appendTo( $('<li></li>') )
)
).append( $('<li></li>')
.append($('<input type="button"></input>')
.attr({
id : 'downButton',
value : '▼'
}).appendTo( $('<li></li>') )
)
)
);
var timeoutId = 0;
$('#upButton' , spinnerControl).mousedown(function() {
console.log('mouse down');
timeoutId = setInterval(self.rotateSpinner('spinner' , true ) , 200);
}).bind('mouseup mouseleave', function() {
console.log('mouse left');
//clearTimeout(timeoutId);
});
// $('#downButton').mousedown(function() {
// alert('herer');
// //timeoutId = setInterval("rotateSpinner('spinner' , false ) ", 200);
// }).bind('mouseup mouseleave', function() {
// clearTimeout(timeoutId);
// });
return spinnerControl;
};
//class END
}
Depends on how you call this function. 取决于您如何调用此函数。
If you call it like this: 如果您这样称呼它:
Spinner().rotateSpinner(arg1, arg2);
You would the error you mentioned. 您将遇到您提到的错误。
The right usage is: 正确的用法是:
new Spinner().rotateSpinner(arg1, arg2);
It's better you learn more about variable scope of javascript. 最好您进一步了解javascript的可变范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.