基本问题,但我无法弄清楚:(。一个解决方案会使另一个解决方案崩溃。这是缩小的具体案例,任何帮助都会受到赞赏。
function onOpen() { // first entry point
var helper = new level1Function();
helper.level2FunctionA();
}
function onFormSubmit() { // second entry point
var helper = new level1Function();
helper.level2FunctionC();
}
function level1Function() {
this.level2FunctionA = function() {
console.log('hi');
}
function level2FunctionB() {
// how do I invoke level2FunctionA from here w/o breaking onOpen entry point?
}
this.level2FunctionC = function() {
level2FunctionB();
}
}
onOpen();
onFormSubmit();
// looking for 2 hi's to the console, one through each flow
最佳答案
创建对变量 self 的引用,并在函数体顶部分配给 this
function level1Function() {
var self = this;
this.level2FunctionA = function() {
console.log('hi');
}
function level2FunctionB() {
self.level2FunctionA();
}
this.level2FunctionC = function() {
level2FunctionB();
}
}
关于javascript - 如何从 javascript/google 应用程序脚本中的外部函数和同级函数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271739/