为什么不能为document.getElementById()设置别名? [英] Why can't one set an alias to document.getElementById()?

查看:109
本文介绍了为什么不能为document.getElementById()设置别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript函数别名似乎不起作用

将document.getElementById设置为变量

代码将如果可能的话,效率更高:

Code would be more efficient if this was possible:

var min = document.getElementById;

然后调用 document.getElementById()使用 min()。

不尝试编写缩小代码但在这种特殊情况下可以减少范围查找和缩短一些行。

Not trying to write minified code but in this particular case one can reduce scope lookup and shorten some lines.

这是语法问题还是语言限制?

Is this a syntax issue or a limiation on the language?

推荐答案

当你打电话给 foo.bar()然后这个设置为 foo 栏内
foo.bar 复制到 window.bar 然后调用 window.bar()是设置为窗口

When you call foo.bar() then this is set to foo inside bar When you copy foo.bar to window.bar then call window.bar(), this is set to window.

getElementById 必须操作一个DOM文档对象。

getElementById has to operate on a DOM document object.

这篇关于为什么不能为document.getElementById()设置别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆