相关疑难解决方法(0)

252
推荐指数
8
解决办法
19万
查看次数

JavaScript函数别名似乎不起作用

我只是在阅读这个问题并且想尝试别名方法而不是函数包装方法,但我似乎无法在它们的调试窗口和Firefox 3或3.5beta4或Google Chrome中使用它们.在测试网页中.

萤火虫:

>>> window.myAlias = document.getElementById
function()
>>> myAlias('item1')
>>> window.myAlias('item1')
>>> document.getElementById('item1')
<div id="item1">
Run Code Online (Sandbox Code Playgroud)

如果我把它放在一个网页中,对myAlias的调用会给我这个错误:

uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame :: file:///[...snip...]/test.html :: <TOP_LEVEL> :: line 7" data: no]
Run Code Online (Sandbox Code Playgroud)

Chrome(为了清晰起见插入了>>>):

>>> window.myAlias = document.getElementById
function getElementById() { [native code] }
>>> window.myAlias('item1')
TypeError: Illegal invocation
>>> document.getElementById('item1')
<div id=?"item1">?
Run Code Online (Sandbox Code Playgroud)

在测试页面中,我得到了相同的"非法调用".

难道我做错了什么?其他人可以重现这个吗?

另外,奇怪的是,我只是尝试了它在IE8中工作.

javascript alias closures function

81
推荐指数
2
解决办法
3万
查看次数