什么是JavaScript中嵌套函数的需要和使用 [英] Whats the need and use of nested functions in JavaScript
问题描述
我理解嵌套函数是什么,但我不明白为什么我们甚至首先需要嵌套函数。是否存在只能通过在JavaScript中使用嵌套函数来解决的问题。我看到的所有创建嵌套函数的示例都可以在不在函数内部创建函数的情况下进行编码,并且结果相同。那么哪个问题需要创建嵌套函数,并且只能通过使用嵌套函数来有效地解决。
I understand what a nested function is, but I don't understand why we even need nested functions in the first place. Is there a problem that can only be solved by using nested functions in JavaScript. All the examples I see which create nested function, can be coded without creating a function inside a function and will result the same. So which problem requires creation of nested functions, and can only/efficiently be solved by using nested functions.
推荐答案
核心重要性嵌套函数是作用域生成。我们需要JavaScript中的嵌套函数和作用域来实现以下目的。
The core importance of nested functions is scope generation. We need nested functions and scopes in JavaScript to achieve the following.
- 未污染的全局命名空间
- 模块化功能
- 封装模块
- 防止标识符冲突跨越不同的脚本
- 由于嵌套作用域内的变量符合缩小。
- 它加速标识符解析流程
- Non-Polluted Global Namespace
- Modularization of functionality
- Encapsulate private internal workings of modules
- Prevent collision of identifiers across different scripts
- Smaller script sizes due to the fact that variables inside nested scopes qualify for minification.
- It speeds up the Identifier Resolution Process
这是一个示例模块,显示函数嵌套和范围提供的封装功能:
Here is a sample module that displays the power of encapsulation offered by function nesting and scopes:
var notificationService = (function ($, toastr, undefined) {
var _externals = {},
_jqExtend = $.extend;
/*
* Private Methods
*/
function _showMessage(params) {
params = params || {};
toastr.remove();
if (typeof (params.title) === "undefined")
toastr[params.method](params.msg);
else
toastr[params.method](params.msg, params.title);
}
/*
* Public Members
*/
_externals.clear = function () {
toastr.remove();
};
_externals.showError = function (params) {
params = params || {};
_jqExtend(params, {
method: "error"
});
_showMessage(params);
};
_externals.showInfo = function (params) {
params = params || {};
_jqExtend(params, {
method: "info"
});
_showMessage(params);
};
_externals.showSuccess = function (params) {
params = params || {};
_jqExtend(params, {
method: "success"
});
_showMessage(params);
};
_externals.showWarning = function (params) {
params = params || {};
_jqExtend(params, {
method: "warning"
});
_showMessage(params);
};
return _externals;
})(jQuery, toastr);
上面的代码使我们能够控制要公开的内容。在此特定情况下,通过引用notificationService对象将所有附加到_externals对象的成员公开给全局名称空间。如果不使用作用域,内部成员(_jqExtend和_showMessage)也将附加到窗口对象,并增加浏览器解析标识符引用所需的工作量。
The code above gives us the power to control which things to expose. In this specific case all members attached to the _externals object are exposed to the global namespace via reference to notificationService object. Without using scoping, internal members (_jqExtend and _showMessage) would also be attached to the window object and increase the effort required by the browser to resolve identifier references.
这篇关于什么是JavaScript中嵌套函数的需要和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!