gpt4 book ai didi

javascript - 命名 : createFunctionDelegate() vs createDelegateFunction()?

转载 作者:行者123 更新时间:2023-11-30 13:36:28 24 4
gpt4 key购买 nike

看来我无法在函数的两个名称之间进行选择:

createFunctionDelegate()createDelegateFunction()

如果重要的话,函数的目的是创建一个新函数,在第二个参数的上下文中调用提供的回调函数。例如:

var foo = {
init: function() {
setTimeout(App.createFunctionDelegate(this.method, this));
},

method: function() {}
}

foo.init() 运行时,它会设置一个超时调用函数,该函数将执行委托(delegate)给在上下文中调用的另一个函数 (this.method) this (foo).

无论如何,我不确定我应该用什么方式命名这个函数。这对我很重要,因为我将在数百个地方使用它,有时我会打一个,偶尔打另一个。这必须改变,我必须做出选择。

最佳答案

我不会使用这些。 bind()会提供你想做的事在 ES5 中。如果它不存在,我将定义 Function.prototype.bind,如 here 所述(但请仔细阅读描述和可能存在的缺点)。

通过这种方式,您可以确保在支持的情况下使用 native 功能。

关于javascript - 命名 : createFunctionDelegate() vs createDelegateFunction()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4639519/

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