我编写的这段代码(这是一个片段)似乎不起作用。我已将其隔离到此处。
grab = window.document.getElementById;
grab("blueBox") // i.e. grab("blueBox").onclick [...]
是否可以在 JavaScript 中创建对 native 函数的引用。我正在对抓取的元素做一些事情,例如我只是将其遗漏了。抓取功能好像不起作用。
我使用的是 FireFox 的最新版本
最佳答案
您这样做的方式会扰乱函数的 this
值的分配。
grab = window.document.getElementById;
grab("blueBox") // i.e. grab("blueBox").onclick [...]
这里this
将是global
对象。尝试:
grab.apply(window.document, ["blueBox"])
或在较新的浏览器中:
grab = window.document.getElementById.bind(window.document);
直接定义this
是什么。
关于Javascript 创建对 native 函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9716389/