为什么类是JavaScript中的保留字? [英] why is class a reserved word in JavaScript?

查看:158
本文介绍了为什么类是JavaScript中的保留字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用JavaScript实现类继承接口。 class 是构造函数名称的完美单词。

I am planning to implement class inherit interface using JavaScript. class is the perfect word as the name of the constructor.

class = function (classname) {}; // create a class with classname
classA = new class("classA"); // create a class named classA

不幸的是, class 是JavaScript中的保留字。

Unfortunately, class is a reserved word in JavaScript.

为什么JavaScript保留单词 class (因为它从不使用它)?

Why does JavaScript reserve the word class (since it never uses it)?

推荐答案

它保留给面向未来的ECMAScript


以下字词是在建议的扩展中用作关键字,因此保留以允许将来采用这些扩展。

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions.

尽管不要担心如果您在JavaScripts中使用最佳实践,则将所有可访问的函数/变量/构造函数放在命名空间中,这样您就可以使用任何您想要的名称:

Don't fret though, if you're using best-practices in your JavaScripts, you're placing all accessible functions/variables/constructors in a namespace, which will allow you to use whatever name you'd like:

foo = {};
foo['class'] = function(){...code...};
var myClass = new foo['class']();

这篇关于为什么类是JavaScript中的保留字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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