在javascript中,如何从同一类中的另一个方法调用类方法? [英] In javascript, how do I call a class method from another method in the same class?

查看:135
本文介绍了在javascript中,如何从同一类中的另一个方法调用类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

  var Test = new function(){
this.init = new function ){
alert(hello);
}
this.run = new function(){
//调用init here
}
}
pre>

我想在运行中调用 init 。 c。使用 this.init() ,但这不是唯一的问题。不要在内部函数上调用新的。

  var Test = new function(){
this.init = function(){
alert(hello);
};

this.run = function(){
//调用init here
this.init();
};
}

Test.init();
Test.run();

//等等


I have this:

var Test = new function() {  
    this.init = new function() {  
        alert("hello");  
    }
    this.run = new function() {  
        // call init here  
    }  
}

I want to call init within run. How do I do this?

解决方案

Use this.init(), but that is not the only problem. Don't call new on your internal functions.

var Test = new function() {
    this.init = function() {
        alert("hello");
    };

    this.run = function() {
        // call init here
        this.init();
    };
}

Test.init();
Test.run();

// etc etc

这篇关于在javascript中,如何从同一类中的另一个方法调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆