gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:31:20 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com