有一个匿名函数采用三个参数(root
, ns
, factory
),它会立即被调用。
root
取`window 的值。
ns
取值为'detectZoom'
factory
取回调函数的值(也是匿名的)
说明:
(function (root, ns, factory) {
// the body of the anonymous function
} (window, 'detectZoom', function() {
// the body of the 'factory' callback
}));
要将其分开,如何通过四个步骤获取此代码:
1.
// Anonymous function.
(function (root, ns, factory) {/* body */});
2.
// Anonynmous function, immediately invoked
(function (root, ns, factory) {/* body */})(); // parentheses mean it's invoked
3.
// Callback as a separate argument
var cbk = function () {};
(function (root, ns, factory) {/* body */})(window, 'detectZoom', cbk);
4.
// Callback as an anonymous function
(function (root, ns, factory) {/* body */})(window, 'detectZoom', function () {});
您可以将代码重写为更详细:
var outer = function (root, ns, factory) {
// the body
};
var callback = function () {
// the body
};
outer(window, 'detectZoom', callback);