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

会有一股神秘感。 提交于 2019-12-11 03:37:54

问题


Some SPECIAL words such as Object, Array, Function, Method, Number etc are not belong to keywords in Javascrpt: Reserved Keywords in Javascript.

But I have to use them carfully, they are not NORMAL words as object, array, method, number, foo ...

I'd like to know how many such SPECIAL words we have? Pls give me the list.


回答1:


Just to clarify, "function" is a reserved word, "Function" is a predefined object in the global scope.

The special words you list (although I'm not sure about "Method"?) are predefined JavaScript Classes and Objects in the global scope. 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. This is a sub-list of the list provided at 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?

Other objects defined by the (Mozilla) browser/DOM are listed in the Gecko DOM Reference.




回答2:


It all boils down to one thing, really: JavaScript is case sensitive. That's why it makes a distinction between Object and object.

Object, Array, Function and Number are not keywords, nor are they exactly "special" (whatever you think they mean) words.

They're nothing more than built-in function/class types in JavaScript (you can do a typeof on them and see). 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. Unless, of course, you give them meaning yourself by declaring variables with those names.



来源:https://stackoverflow.com/questions/3896744/why-javascript-doesnt-include-reserved-keywords-such-as-object-array-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!