简体   繁体   English

为什么Javascript不包含“ Object”,“ Array”,“ Function”,“ Number”等保留关键字?

[英]Why Javascript doesn't include reserved keywords such as “Object”, “Array”, “Function”, “Number” …?

Some SPECIAL words such as Object , Array , Function , Method , Number etc are not belong to keywords in Javascrpt: Reserved Keywords in Javascript . 一些特殊的单词,例如ObjectArrayFunctionMethodNumber等不属于Javascrpt中的关键字: Javascript中的保留关键字

But I have to use them carfully, they are not NORMAL words as object , array , method , number , foo ... 但是我必须小心使用它们,它们不是objectarraymethodnumberfoo等普通单词。

I'd like to know how many such SPECIAL words we have? 我想知道我们有多少个这样的特殊单词? Pls give me the list. 请把清单给我。

It all boils down to one thing, really: JavaScript is case sensitive. 这实际上归结为一件事: JavaScript是区分大小写的。 That's why it makes a distinction between Object and object . 这就是为什么它区分Objectobject

Object , Array , Function and Number are not keywords, nor are they exactly "special" (whatever you think they mean) words. ObjectArrayFunctionNumber不是关键字,也不是完全“特殊”(无论您认为它们是什么意思)的词。

They're nothing more than built-in function/class types in JavaScript (you can do a typeof on them and see). 它们不过是JavaScript中的内置函数/类类型(您可以对它们进行typeof看看)。 You don't directly use them often now though since there are syntactic alternatives to creating objects of each of those types, for example: 您现在不经常直接使用它们,因为存在创建每种类型的对象的语法替代方法,例如:

var obj = {};
var func = function() {};
var arr = [];
var num = 123;

The others you mention ( object , array , method , number , foo ) aren't keywords or "special" words either simply because, since as I say JavaScript is case sensitive, they mean nothing in JavaScript compared to their capitalized counterparts. 您提到的其他objectobjectarraymethodnumberfoo )既不是关键字也不是“特殊”单词,这仅仅是因为(正如我所说的JavaScript区分大小写),与大写字母相比,它们在JavaScript中没有任何意义。 Unless, of course, you give them meaning yourself by declaring variables with those names. 当然,除非您通过使用这些名称声明变量来赋予它们自己的含义。

Just to clarify, " function " is a reserved word, " Function " is a predefined object in the global scope. 为了澄清起见,“ function ”是保留字,“ Function ”是全局范围内的预定义对象。

The special words you list (although I'm not sure about "Method"?) are predefined JavaScript Classes and Objects in the global scope. 您列出的特殊词(尽管我不确定“方法”吗?)是全局范围内的预定义JavaScript类和对象 They aren't necessarily reserved words because they aren't part of the language syntax and can, in some cases, be overridden. 它们不一定是保留字,因为它们不是语言语法的一部分,在某些情况下可以被覆盖。 But yes, ordinarily they should not be used and should otherwise be treated the same way as 'reserved words'. 但是,是的,通常不应该使用它们,否则应将它们与“保留字”一样对待。 See also Global Properties and Methods . 另请参见全局属性和方法

EDIT: With reference to the list provided at developer.mozilla.org/en/JavaScript/Reference/Global_Objects - this appears to be a list of the core JavaScript Objects, irrespective of whether the JavaScript engine is running in the browser or not. 编辑:参考developer.mozilla.org/en/JavaScript/Reference/Global_Objects中提供的列表-这似乎是核心 JavaScript对象的列表,而不管浏览器中是否运行了JavaScript引擎。 This is a sub-list of the list provided at About.com. 这是About.com提供的列表的子列表。 Although why 'Boolean' is omitted from the list of Global Objects at About.com I don't know - this does appear to be an omission? 虽然我不知道为什么从About.com的“全局对象”列表中省略了“布尔”,但这确实是一个省略?

Other objects defined by the (Mozilla) browser/DOM are listed in the Gecko DOM Reference . (Mozilla)浏览器/ DOM定义的其他对象在Gecko DOM参考中列出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM