Javascript Window.Onload 函数链
只是为了进行实验,我一直在尝试确定在网络浏览器中非破坏性链接 window.onload
函数的不同方法。 这是我到目前为止的想法:
var load = window.onload;
var newFunction = function(){
alert("ha!");
}
window.onload = function(){
load();
newFunction();
}
我看到的问题是,每次链接一个函数时,它都会向堆栈添加另一层函数调用。 有没有更好的方法来解决这个问题,而不会给调用堆栈增加不必要的深度?
Just for the sake of experimentation, I've been trying to determine different ways to non-destructively chain window.onload
functions in a web browser. This is the idea of what I have so far:
var load = window.onload;
var newFunction = function(){
alert("ha!");
}
window.onload = function(){
load();
newFunction();
}
The problem I see with this is that every time you chain a function, it adds another level of function calls to the stack. Is there a better way to go about this that doesn't add unnecessary depth to the call stack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 addEventListener/attachEvent 可能会更好吗?
高级事件注册模型
May be it will be better to use addEventListener/attachEvent?
Advanced event registration models
你可以看看 jQuery 他们是如何处理这个问题的。
来自 jQuery 文档:
You could have a look at jQuery how they handle that.
From the jQuery docs:
我宁愿使用“足够好”的 addEvent:
来自: http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
I'd rather use the "good enough" addEvent:
From: http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/