在Javascript类中调用另一个方法
在Javascript中定义一个类时,如何从另一个方法调用一个方法?
exports.myClass = function () { this.init = function() { myInternalMethod(); } this.myInternalMethod = function() { //Do something } }
上面的代码在执行时给了我下面的错误:
ReferenceError:myInternalMethod未定义
我也试过this.myInternalMethod和self.myInternalMethod,但都导致错误。 什么是正确的方法来做到这一点?
我创build了这个小提琴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();
this.myInternalMethod()
似乎工作,虽然:
var exports = {}; exports.myClass = function () { this.init = function() { this.myInternalMethod(); } this.myInternalMethod = function() { //Do something } } var x = new exports.myClass(); x.init();
是私人会员吗?
exports.myClass = function () { this.init = function() { myInternalMethod(); } function myInternalMethod() { //Do something } }