最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript shim to redefine "const" as "var" for Internet Explorer - Stack Overflow

programmeradmin0浏览0评论

Internet Explorer doesn't support the "const" keyword. Can I use a shim that checks if "const" is supported and, if not, redefines it as var? I guess it would be nice if it enforced the constancy, maybe using object.Freeze, but I'd be okay with a simple shim.

UPDATE: I want this so I can use existing Javascript libraries that use "const" without modifying them. Obviously, find / replace would work, but it's messy and not very maintainable.

Internet Explorer doesn't support the "const" keyword. Can I use a shim that checks if "const" is supported and, if not, redefines it as var? I guess it would be nice if it enforced the constancy, maybe using object.Freeze, but I'd be okay with a simple shim.

UPDATE: I want this so I can use existing Javascript libraries that use "const" without modifying them. Obviously, find / replace would work, but it's messy and not very maintainable.

Share Improve this question edited Jul 5, 2011 at 19:04 Jon Galloway asked Jul 5, 2011 at 18:30 Jon GallowayJon Galloway 53.2k25 gold badges127 silver badges194 bronze badges 5
  • 7 If it doesn't matter for your code whether the identifier is constant or not (since you don't mind giving IE var), why not just make all the identifiers variables? – lonesomeday Commented Jul 5, 2011 at 18:33
  • 7 @lonesomeday - same reason const was invented. It documents the intent of the code better than var and, when const is available, it enforces that intent too. He's trying to use language advantages when available, not just program to the lowest mon denominator. Makes sense to me. – jfriend00 Commented Jul 5, 2011 at 18:49
  • Added some info - the main reason is that I want to be able to use libraries other people have written without modifying them. – Jon Galloway Commented Jul 5, 2011 at 19:05
  • js2coffee brought it to mind, but I'd like to know for when I run across others. – Jon Galloway Commented Jul 5, 2011 at 19:09
  • lonesomeday - Thanks, but that doesn't solve the problem of using other libraries without modifying them. – Jon Galloway Commented Jul 6, 2011 at 18:51
Add a ment  | 

2 Answers 2

Reset to default 1

You could write a server-side shim, so when the .js file is requested any const is replaced by var when the file is streamed to the browser. (Appropriate word-break / whitespace detection needed)

Since you're on the server you can't detect browser capabilities and would have to depend on the User-Agent string to do this for IE only, or have javascript that runs on the client browser tell the server that it needs non-const code.

I do a similar thing with a "userprefs.css" file that is really backed by a Java servlet, building and streaming the file specifically for each user.

I would use Modernizer for this and load the appropriate script file based on support for Const. You could probably write a Grunt JS plugin that builds out the second version for IE automatically by doing a automatic find and replace for 'const'.

发布评论

评论列表(0)

  1. 暂无评论