javascript - 如何从 javascript/google 应用程序脚本中的外部函数和同级函数调用函数

标签 javascript function google-apps-script parent siblings

基本问题,但我无法弄清楚:(。一个解决方案会使另一个解决方案崩溃。这是缩小的具体案例,任何帮助都会受到赞赏。

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/

相关文章:

arrays - 如何在 perl 中以字节为单位获取数组的大小?

javascript - 如何在 Vue 中使用全局函数?

google-apps-script - Google Apps脚本更改Google工作表中特定单元格的背景颜色

google-apps-script - 从工作表图表中提取替代文本 - Apps 脚本

javascript - slideToggle() 选择第一个状态

javascript - 如何提取文本区域内容的第一行

javascript - 以这种方式在原型(prototype)上定义函数有什么缺点?

javascript - 忽略其中包含公式的空单元格值

javascript - 围绕 Canvas 圆绘制条形音箱

javascript - 如何替换 Angular 2中的字符串?