Javascript、别名和 IE

发布于 2024-08-19 09:29:26 字数 476 浏览 5 评论 0原文

为了节省空间,我在代码中为函数创建了一大堆别名。它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-08-26 09:29:26

这是不能直接回答您的问题的答案之一,但我认为它会有所帮助:

您应该编写没有所有短变量和别名的代码。将其完整地写下来,因为这样会更容易维护。

然后,当您将代码转移到生产环境时,将您的代码放入一个压缩器中,该压缩器会为您执行别名和变量缩短。

您可以尝试这两个中的任何一个来为您完成(还有很多其他好的):

  1. Google Closure 编译器
  2. Dean Edwards Packer(启用“收缩变量”)

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):

  1. Google Closure Compiler
  2. Dean Edwards Packer (Turn on 'Shrink variables')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文