JavaScript:firefox 拒绝 class 关键字,而 chrome 则不然

发布于 2024-12-03 21:34:20 字数 425 浏览 0 评论 0原文

我正在尝试这段 JavaScript 代码:

class User(name) {
    this.name = name;
}

var class = new User("Kimo");

上面的代码是在 JavaScript 书中按原样使用的,我只是在玩它。

firefox 6.0.2(来自控制台)拒绝将“class”关键字作为变量名(抱怨语法错误),而 chrome 13.0.782.220m(来自开发人员工具)根本没有抱怨。

这让我思考,哪一个是正确的?由于 JavaScript 中不存在类的概念,或者它与其他 OOP 语言不同。另一方面,阻止开发人员使用它可能是更明智的做法。

我想知道为什么 Firefox 和 Chrome 之间有这种不同的方法(我知道他们使用不同的引擎)。

谢谢

I was trying this JavaScript piece of code:

class User(name) {
    this.name = name;
}

var class = new User("Kimo");

The above was used as is in a JavaScript book, and I was simply playing with it.

firefox 6.0.2 (from the console) refused the 'class' keyword as a variable name (complained about a syntax error), while chrome 13.0.782.220m (from the developer tool) didn't complain at all.

It makes me think, which one is right? Since the notion of a class in JavaScript doesn't exist or it is different than other OOP languages. On the other hand, it might be wiser to prevent developers from using it.

I would like to know why this different approaches between firefox and chrome (I know they use different engines).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

独守阴晴ぅ圆缺 2024-12-10 21:34:20

首先,你不能像这样在 Javascript 中创建一个。创建一个对象:

var User = {...};

或构造函数:

function User() {...}

classabout 的保留字.com(有些未在 ECMA 262 中列出,但存在于 JScript 中)和 MDC - 保留供将来使用。确实,JavaScript 目前还没有类。但这个词是保留的,因为有一天它可以有这样的含义。

Mozilla 比其他浏览器对规则更加严格,并且会给出语法错误。

另外:

JavaScript 并不是纯粹的面向对象编程语言(OOP),它是基于原型的编程语言

另一方面,阻止开发人员使用它可能更明智。

由于JavaScript是由浏览器解释的,所以我认为不能禁止保留字的使用。在编译后的代码中,在产品准备好使用之前,编译器会在这些事情上与您意见不一致。

另一件事 - 你可以在编辑器上编写 JavaScript 代码,但没有能力告诉你,你的代码有错误,或者你使用了变量的保留字。

First of all you can`t create a class in Javascript like this. Either create an object:

var User = {...};

or a constructor function:

function User() {...}

class is reserved word from about.com (some not listed in ECMA 262, but present in JScript) and MDC - reserved for future use. It is true that JavaScript has no classes for the moment. But the word is reserved in the sense that some day it can have such.

Mozilla is more strict about the rules than other browsers and gives a syntax error.

In addition :

JavaScript is not purely Object Oriented Programming language ( OOP ) it is Prototype-based Programming language.

On the other hand, it might be wiser to prevent developers from using it.

As JavaScript is interpreted by the browser, I don`t think the usage of reserved words can be prohibited. In the compiled code you have the compiler to disagree with you about such things, before the product is ready to use.

One other thing - you can write JavaScript code on editor with no capabilities to tell you, that your code has errors, or that you use reserved words for variables.

枕头说它不想醒 2024-12-10 21:34:20

错误是正确的行为。 Class 是 ECMAScript 中的限制关键字。

An error is correct behavior. Class is a restricted keyword in ECMAScript.

美煞众生 2024-12-10 21:34:20

Firefox 还没有实现类支持,如果你检查 ecma6 浏览器支持 又名 javascript(它是新版本将具有更多功能,例如 class :D),您会发现 Mozilla 的 Class 是不可能的,希望很快就会可用,ecma 6 处于测试阶段,所以还需要一段时间才能实现所有功能浏览器支持所有功能

Firefox didnt implement Class support yet, if you check ecma6 browser support aka javascript(its a new realease that will have more feactures such as class :D), you will see that Class for mozilla isnt possible, lets hope soon will be available, ecma 6 is in beta stage, so it will be a while till all browsers support all the feactures

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文