简体   繁体   English

使用命名函数进行回调

[英]Using named functions for callbacks

I have a major problem with profiling in javascript with anonymous functions, I have always many anonymous functions - most of them are callbacks - and It makes analyzing results of profiler very hard for me.我在使用匿名函数进行 javascript 分析时遇到了一个主要问题,我总是有很多匿名函数 - 其中大多数是回调 - 这使得分析分析器的结果对我来说非常困难。

Finally I decided to use named functions for callbacks, like this:最后我决定使用命名函数进行回调,如下所示:

var f = function(callback) {
    // Do something ...
    callback();
}

f(function named_function() {
    console.log('Sample callback function!');
});

I want to know that will I have any problems after making this change in my codes?我想知道在我的代码中进行此更改后我会遇到任何问题吗? And will this type of function definition and passing reserve the name (named_function) anywhere?这种类型的函数定义和传递会在任何地方保留名称 (named_function) 吗?

The name will only be available inside the scope of the named function expression.该名称仅在命名函数表达式的范围内可用。

But there is a problem in IE 8 and lower.但是在 IE 8 及更低版本中存在问题。 It will leak out to the outer scope, and will actually create a different function object, so you should nullify it if that's a problem.它会泄漏到外部作用域,并且实际上会创建一个不同的函数对象,因此如果这是一个问题,您应该将其取消。

f(function named_function() {
    console.log('Sample callback function!');
});
var named_function = null;

See this article for more information: Named function expressions demystified有关更多信息,请参阅此文章:命名函数表达式揭秘

Or you could create it like this to solve the IE issue.或者你可以像这样创建它来解决 IE 问题。

f(function() {
    return function named_function() {
        console.log('Sample callback function!');
    };
}());

But that's a little ugly.但这有点难看。

If you pass anonymous functions like that, the name will exist inside the function itself.如果您传递这样的匿名函数,该名称将存在于函数本身中。

It will not exist in any other scope.它不会存在于任何其他范围内。

var f = function(callback) {
    // Do something ...
    callback();
}

f(function named_function() {
    console.log(named_function); // Logs the function
    console.log('Sample callback function!');
});

console.log(named_function);​ // Error: named_function is undefined

You don't have to complicate things.你不必把事情复杂化。

Just name the function when you declare it只需在声明函数时为其命名

var foo = function foo(){};

Defining a named callback in a scope will cause it to be visible only in that scope.在范围内定义命名回调将使其仅在该范围内可见。 I would therefore conclude that there shouldn't be any naming conflicts.因此,我得出的结论是不应该有任何命名冲突。 For example, the following code runs as expected:例如,以下代码按预期运行:

(function named_callback() { console.log("Callback 1"); })();
(function named_callback() { console.log("Callback 2"); })();​

Actually, you're still creating an anonymous function expression and assigning it to a local scoped variable f .实际上,您仍在创建匿名函数表达式并将其分配给局部作用域变量f Go for去做

function f( callback ) {
    callback();
}

f( named_function );

function named_function() {
    console.log('Sample callback function!');
}

That way, you're even avoiding the named function expression memory leak in <= IE8, plus since you're no longer creating a function expression but a *function declaration, you can even access f within the body.这样,您甚至可以避免 <= IE8 中的命名函数表达式内存泄漏,而且由于您不再创建函数表达式而是创建 *function 声明,因此您甚至可以在正文中访问f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM