I have problem calling one method from another method in a Javascript class.
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.
Why is it appearing ?
-
stackoverflow.com/questions/2233710/…Naveen Kumar Alone– Naveen Kumar Alone2013年07月08日 06:18:00 +00:00Commented Jul 8, 2013 at 6:18
-
I had already seen the post but haven't figure out the problem yet, rotateSpinner is not called. I haven't used new operator as suggested in that question.Taha Iqbal– Taha Iqbal2013年07月08日 06:21:25 +00:00Commented Jul 8, 2013 at 6:21
-
try to define rotateSpinner variable to outside of function as a gloabl variable.Devang Rathod– Devang Rathod2013年07月08日 06:27:43 +00:00Commented Jul 8, 2013 at 6:27
2 Answers 2
this is referring the DOM-element, which in this case is the '#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.
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
}
1 Comment
var that = this.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.