简体   繁体   English

从另一个方法调用javascript类方法

[英]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 : '&#9650;'
                                }).appendTo( $('<li></li>') )
                            )   


                        ).append( $('<li></li>')
                            .append($('<input type="button"></input>') 
                                    .attr({
                                        id    : 'downButton',
                                        value : '&#9660;'
                                    }).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 : '&#9650;'
                            }).appendTo( $('<li></li>') )
                        )   


                    ).append( $('<li></li>')
                        .append($('<input type="button"></input>') 
                                .attr({
                                    id    : 'downButton',
                                    value : '&#9660;'
                                }).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.

相关问题 从JavaScript中的另一个类调用一个类方法? - Call a class method from a another class in JavaScript? 使用JavaScript / jQuery从另一个类调用C#方法 - Call C# method from another class using JavaScript/jQuery 在javascript中,如何从同一个类中的另一个方法调用类方法? - In javascript, how do I call a class method from another method in the same class? 另一种方法是Javascript调用方法 - Javascript call method in another method 如何在Javascript类中的另一个方法内调用一个方法? - How to call a method inside another method in a class in Javascript? Javascript - 如何在同一个类中的另一个方法中调用一个方法 - Javascript - How to call a method in another method within the same class Javascript:Phaser 在另一个方法类中调用一个方法 - Javascript : Phaser call a method inside another method class OOP javascript,从AJAX“声明”中的方法中,如何调用该类的另一个方法? - OOP javascript, from a AJAX 'statement' in a method, how call an another method of the class? 从另一个方法内部调用 javascript 类方法,导致“moveToCell 不是函数”错误 - javascript class method call from inside another method, results in "moveToCell is not a function" error 从另一个类的公共方法中调用一个类的公共方法 - Call a public method of a class, from a public method of another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM