Javascript 了解如何调用ready中的匿名函数
我刚刚看过这段代码,它描述了如何在纯js中创建自己的定制就绪函数。答案解释得非常详细,我已经用js编程一段时间了,但在理解代码的初始部分时仍然存在问题,请再次查看下面的代码:Javascript 了解如何调用ready中的匿名函数,javascript,Javascript,我刚刚看过这段代码,它描述了如何在纯js中创建自己的定制就绪函数。答案解释得非常详细,我已经用js编程一段时间了,但在理解代码的初始部分时仍然存在问题,请再次查看下面的代码: (function(funcName, baseObj) { // The public function name defaults to window.docReady // but you can pass in your own object and own function name and th
(function(funcName, baseObj) {
// The public function name defaults to window.docReady
// but you can pass in your own object and own function name and those will be used
// if you want to put them in a different namespace
funcName = funcName || "docReady";
baseObj = baseObj || window;
var readyList = [];
var readyFired = false;
var readyEventHandlersInstalled = false;
// call this when the document is ready
// this function protects itself against being called more than once
function ready() {
if (!readyFired) {
// this must be set to true before we start calling callbacks
readyFired = true;
for (var i = 0; i < readyList.length; i++) {
// if a callback here happens to add new ready handlers,
// the docReady() function will see that it already fired
// and will schedule the callback to run right after
// this event loop finishes so all handlers will still execute
// in order and no new ones will be added to the readyList
// while we are processing the list
readyList[i].fn.call(window, readyList[i].ctx);
}
// allow any closures held by these functions to free
readyList = [];
}
}
function readyStateChange() {
if ( document.readyState === "complete" ) {
ready();
}
}
// This is the one public interface
// docReady(fn, context);
// the context argument is optional - if present, it will be passed
// as an argument to the callback
baseObj[funcName] = function(callback, context) {
// if ready has already fired, then just schedule the callback
// to fire asynchronously, but right away
if (readyFired) {
setTimeout(function() {callback(context);}, 1);
return;
} else {
// add the function and context to the list
readyList.push({fn: callback, ctx: context});
}
// if document already ready to go, schedule the ready function to run
if (document.readyState === "complete") {
setTimeout(ready, 1);
} else if (!readyEventHandlersInstalled) {
// otherwise if we don't have event handlers installed, install them
if (document.addEventListener) {
// first choice is DOMContentLoaded event
document.addEventListener("DOMContentLoaded", ready, false);
// backup is window load event
window.addEventListener("load", ready, false);
} else {
// must be IE
document.attachEvent("onreadystatechange", readyStateChange);
window.attachEvent("onload", ready);
}
readyEventHandlersInstalled = true;
}
};
})("docReady", window);
我的问题是,你怎么能像这样调用匿名函数。。我完全糊涂了:(
下面的代码究竟是如何工作的
docReady(function() {
alert('hello');
}, window);
我的意思是没有定义明确的docReady函数,如下所示:
docReady function (param1, param2);
我看到的只是docReady作为参数传递给匿名函数
baseObj[funcName] = function(callback, context) {
相当于
window["docReady"] = function(callback, context) {
它将函数声明为全局对象窗口的属性,这意味着您可以使用
window["docReady"](function() {
alert('hello');
}, window);
或与
window.docReady(function() {
alert('hello');
}, window);
甚至
docReady(function() {
alert('hello');
}, window);
由于全局对象的属性也是全局范围的变量(以及隐藏之前的任何范围)。关键位是baseObj[funcName]=function
此时,baseObj
是(或至少可以是)window
,funcName
是(或可以是)docReady
因此,在这一点上,它向窗口(全局)添加了一个名为“docReady”的函数
窗口
和“docReady”在最后一行作为默认参数传递
(“docReady”,窗口)代码>
在顶部输入函数作为参数(函数(funcName,baseObj){
N.B.当我说baseObj
是(或至少可以是)window
时,这是因为您可以覆盖此值,这一行:
funcName = funcName || "docReady";
baseObj = baseObj || window;
将的funcName
设置为“docReady”和baseObj
,如果没有提供备选方案,这意味着您可以根据需要更改它们,如果最后一行更改为}(“getReady”,myObject)代码>该函数将被调用为getReady,并被添加到myObject
,而不是全局窗口
您读取的代码是错误的
检查以下行:
(function(funcName, baseObj) {
...
baseObj[funcName] = function(callback, context) {
...
};
...
})("docReady", window);
它正在向baseObj
添加一个新属性,在本例中是窗口
。该属性就是您调用的函数。docReady
。所有全局的东西都不需要像window.something那样调用。这就是为什么您使用docReady
baseObj[funcName]
声明函数(编辑:我做了一个回答,让它更清楚)。他说了什么。在上面的代码中,baseObj
是window
,而funcName
是“docReady”
,因此它是window[“docReady”]=function()…
,这与window.docReady=function()…
(function(funcName, baseObj) {
...
baseObj[funcName] = function(callback, context) {
...
};
...
})("docReady", window);