问题
I was studying the Twitter source code, and I came across the following snippet:
window.setTimeout=window.setTimeout;window.setInterval=window.setInterval;
Why does Twitter redefine these functions?
Edit: To see the code, go to any twitter user page, open the source of the page, and you will see that snippet in the second block of javascript.
回答1:
This is a technique to replace setTimeout
and setInterval
functions globally in a cross-browser fashion.
window.setTimeout
, when used as an lvalue (on the left side of the assignment), does not walk the prototype chain, but on the right side, it does. So this will always pull a property out of the prototype chain and put it right on the object.
See http://www.adequatelygood.com/2011/4/Replacing-setTimeout-Globally.
来源:https://stackoverflow.com/questions/8044433/why-does-twitter-redefine-window-settimeout-and-window-setinterval