2

Javascript でクラスを定義するとき、あるメソッドから別のメソッドを呼び出すにはどうすればよいですか?

exports.myClass = function () {

    this.init = function() {
        myInternalMethod();
    }

    this.myInternalMethod = function() {
        //Do something
    }
}

上記のコードを実行すると、次のエラーが表示されます。

ReferenceError: myInternalMethod が定義されていません

this.myInternalMethod と self.myInternalMethod も試しましたが、どちらもエラーになります。これを行う正しい方法は何ですか?

4

3 に答える 3

6

私はこのフィドルを作成しましたhttp://jsfiddle.net/VFKkC/ここで myInternalMedod() を呼び出すことができます

var myClass = function () {

    this.init = function() {
        this.myInternalMethod();
    }

    this.myInternalMethod = function() {
        console.log("internal");
    }
}

var c = new myClass();

c.init();
于 2013-12-01T10:41:44.657 に答える
0

this.myInternalMethod()ただし、機能しているようです:

var exports = {};
exports.myClass = function () {

    this.init = function() {
        this.myInternalMethod();
    }

    this.myInternalMethod = function() {
        //Do something
    }
}

var x = new exports.myClass();
x.init();
于 2013-12-01T10:41:34.293 に答える
0

プライベート会員ですか?

exports.myClass = function () {

    this.init = function() {
        myInternalMethod();
    }

    function myInternalMethod() {
        //Do something
    }
}
于 2013-12-01T10:42:16.403 に答える