词汇这在consis方法在Javascript中

下面是我的解释代码。 我有一个类的成员函数和variables如下。 functionOne&functionTwo简洁的方法。

function MyUtils() { this.actions = { functionOne(param1, param2) { console.log('This is first function which is not in action'); }, functionTwo(param1, param2) { console.log('This is second function calling function three'); //HOW DO I CALL functionThree HERE? // I tried doing - this.functionThree() but not working } } this.functionThree() { console.log('This is third function'); } } 

如果函数二被调用,那么我想函数三是在它的callen?

你可以在不使用这个关键字的情况下做到这一点,

 function MyUtils() { function functionThree() { console.log('This is third function'); } this.actions = { functionOne(param1, param2) { console.log('This is first function which is not in action'); }, functionTwo(param1, param2) { console.log('This is second function calling function three'); //HOW DO I CALL functionThree HERE? // I tried doing - this.functionThree() but not working functionThree(); } } } 

这里是repl的输出:(在这里find)

 clear Native Browser JavaScript This is second function calling function three This is third function => undefined 

尝试这个:

 function MyUtils() { var that = this; this.actions = { functionOne(param1, param2) { console.log('This is first function which is not in action'); }, functionTwo(param1, param2) { console.log('This is second function calling function three'); that.functionThree(); } } this.functionThree = function functionThree() { console.log('This is third function'); } } 

要检查它是否工作:

 var utils = new MyUtils(); utils.actions.functionTwo(); // Will output: // This is second function calling function three // This is third function