如何根据传递的参数在JavaScript函数中命名匿名函数?
我想根据传递的参数给出我函数中的匿名函数的名称。例如,如果传递的参数是“自然”,那么我想创建一个名称的匿名函数。
示例代码:
function hello(nature) {
window.nature /* I want this word "nature" to be taken from the parameter passed by the function */ = function () {
console.log('succes');
}
}
I want to give the anonymous function's name that is inside my function based on the parameters passed. For example, if the passed parameter is "nature" then I want to create an anonymous function with that name.
Example code:
function hello(nature) {
window.nature /* I want this word "nature" to be taken from the parameter passed by the function */ = function () {
console.log('succes');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将字符串作为参数进行操作。
在这里,如何分配自定义名称和自定义功能(如果需要)。
You can do it by passing a string as parameter.
Here how to assign a custom name and a custom function, if needed.
这是一个典范,可变“自然”具有值“ fn”,并且传递给“ FN”的参数已记录到控制台。
Here is an exemple where variable "nature" has value "fn" and parameter passed to "fn" is logged to the console.