“新"的限制Javascript 中的关键字 [英] Restriction of "new" keyword in Javascript

查看:27
本文介绍了“新"的限制Javascript 中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 JS 代码:

I have this JS code:

var A = {};
A.new = function(n) { return new Array(n); }

它在所有浏览器中都运行良好,但是当我尝试使用 obfuscator 对其进行模糊处理时,它显示错误.

It works well in all browsers, but when I try to obfuscate it with obfuscator, it shows an error.

它是有效的 JS 代码吗?我查看了规范,但没有找到任何内容.我知道,浏览器有时会接受语法错误的代码,但我想编写语法正确的代码.

Is it a valid JS code? I looked into specification, but didn't find anything. I know, that browsers sometimes accept syntactically wrong code, but I want to write syntactically correct code.

注意,我不是在做 var new = ...

顺便说一句.这个怎么办?

BTW. what about this?

var a = { "new" : 2 };  // ok?
a.new    = 3;           // ok?
a["new"] = 3;           // ok?

.

谢谢大家的帮助!我给混淆器的作者写了一封电子邮件,他们修复了它!:)

Thank you all for your help! I wrote an email to obfuscator's authors and they fixed it! :)

推荐答案

是的,您的代码是有效的,但混淆器是错误的(或旧的).new 是一个保留字,也就是说它不是一个有效的标识符(例如用作变量名).然而,它仍然是一个在 对象字面量中有效的 IdentifierName并在 点符号属性访问中.

Yes, your code is valid an the obfuscator is wrong (or old). new is a reserved word, that means it is not a valid identifier (e.g. to be used as a variable name). Yet, it is still an IdentifierName which is valid in object literals and in dot-notation property access.

然而,在 EcmaScript 3 中情况并非如此,其中 两者 这些需要Identifiersnew等关键字无效.因此,在 Web 上的脚本中使用这些未加引号的名称被认为是不好的做法,这些名称可能会被旧浏览器执行.

However, this was not the case in EcmaScript 3, where both of these needed to be Identifiers, and keywords like new were invalid. Therefore it is considered bad practise to use these names unquoted in scripts on the web, which might be executed by older browsers.

这篇关于“新"的限制Javascript 中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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