Javascript、别名和 IE
为了节省空间,我在代码中为函数创建了一大堆别名。它在 FF 中工作得很好,但现在我试图添加对 IE 的支持,但它并不高兴。
j=String;
f0=j.fromCharCode;
j=j.prototype;
j.f1=j.indexOf;
j.f2=j.lastIndexOf;
j.f3=j.concat;
j.f4=j.substring;
function alias(c,e){return function(){return c[e].apply(c,arguments);};}
w=window.location;
d=document;
b=document.body;
f5=alias(d,"createElement");
f6=alias(b,"appendChild");
f7=alias(d,"getElementById");
...etc
我确信 alias() 函数在 IE 中不起作用。有没有在两种浏览器中都有效的替代方法?
In an attempt to save space, I made a whole bunch of aliases for functions in my code. It works fine in FF, but now I'm trying to add support for IE and it's not happy.
j=String;
f0=j.fromCharCode;
j=j.prototype;
j.f1=j.indexOf;
j.f2=j.lastIndexOf;
j.f3=j.concat;
j.f4=j.substring;
function alias(c,e){return function(){return c[e].apply(c,arguments);};}
w=window.location;
d=document;
b=document.body;
f5=alias(d,"createElement");
f6=alias(b,"appendChild");
f7=alias(d,"getElementById");
...etc
I know for sure the alias() function doesn't work in IE. Is there an alternative way that works in both browsers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不能直接回答您的问题的答案之一,但我认为它会有所帮助:
您应该编写没有所有短变量和别名的代码。将其完整地写下来,因为这样会更容易维护。
然后,当您将代码转移到生产环境时,将您的代码放入一个压缩器中,该压缩器会为您执行别名和变量缩短。
您可以尝试这两个中的任何一个来为您完成(还有很多其他好的):
This is one of those answers that doesn't directly answer your question, but I think it will help:
You should write your code without all the short variables and aliases. Write it out full form as it will be much easier to maintain.
Then, when you move the code to production, put your code through a minifier that does the aliasing and variable shortening for you.
You could try out either of these two to do it for you (There are lots of other good ones as well):