下面是我的代码并作解释。
我有一个其成员函数和变量如下的类。 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');
}
}
如果调用了函数2,那么我要在其中调用函数3吗?
最佳答案
您可以在没有此关键字的情况下执行此操作,这会在javascript中使用闭包语法:
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的输出:(找到here)
clear
Native Browser JavaScript
This is second function calling function three
This is third function
=> undefined