开发者

Javascript, aliases and IE

In an attemp开发者_运维技巧t 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?


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜