在Javascript类中调用另一个方法

Bor*_*ris 2 javascript methods class node.js referenceerror

在Javascript中定义类时,如何从另一个方法中调用一个方法?

exports.myClass = function () {

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

    this.myInternalMethod = function() {
        //Do something
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在执行时给出了以下错误:

ReferenceError:未定义myInternalMethod

我也尝试过this.myInternalMethod和self.myInternalMethod,但都会导致错误.这样做的正确方法是什么?

Bas*_*ijk 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();
Run Code Online (Sandbox Code Playgroud)