JavaScript rese...LOGIN

JavaScript reserved keywords

JavaScript reserved keywords and dangerous variable names Be careful when defining variable names

JavaScript standards

All modern browsers fully support ECMAScript 3 (ES3, the third version of JavaScript, from 1999).

ECMAScript 4 (ES4) failed.

ECMAScript 5 (ES5, released in 2009), is the latest official version of JavaScript.

Over time, we are starting to see that all modern browsers have full support for ES5.


JavaScript reserved keywords

Javascript reserved keywords cannot be used as variable, label or function names. Some reserved keywords are used by Javascript for future extensions.

abstract arguments boolean break byte

case catch char class* const

continue debugger default delete do

double else enum* eval export*

extends* false final finally float

for function goto if implementations

import* in instanceof int interface

let long native new null

package private protected public return

short static super* switch synchronized

this throw throws transient true

try typeof var void volatile

while with yield

* The keywords marked are newly added in ECMAScript5.


JavaScript Objects, Properties and Methods

You should also avoid using the names of JavaScript’s built-in objects, properties and methods as JavaScript variable or function names:

Array Date eval function hasOwnProperty

Infinity isFinite isNaN isPrototypeOf length

Math NaN name Number Object

prototype String toString undefined valueOf


##Java Reserved Key Word

JavaScript is often used together with Java. There are some Java objects and properties that you should avoid using as JavaScript identifiers:

getClass java JavaArray javaClass JavaObject JavaPackage


Windows Reserved Keywords

JavaScript can be used outside of HTML. It can be used as a programming language in many other applications.

In HTML, you must (and for portability, you should) avoid using the names of HTML and Windows objects and properties as Javascript variable and function names:

alert all anchor anchors area

assign blur button checkbox clearInterval

clearTimeout clientInformation close closed confirm

constructor crypto decodeURI decodeUR IComponent defaultStatus

document element elements embed embeds

encodeURI encodeURIComponent escape event fileUpload

focus form forms frame innerHeight

innerWidth layer layer layers link location

mimeTypes navigate navigator frames frameRate

hidden history image images offscreenBuffering

open opener option outerHeight outerWidth

packages pageXOffset pageYOffset parent parseFloat

parseInt password pkcs11 plugin prompt

propertyIsEnum radio reset screenX screenY

scroll secure select self setInterval

setTimeout status submit taint text

textarea top unescape untaint window


HTML event handler

In addition, you should also Avoid using HTML event handler names as Javascript variable and function names.

Example:

onblur onclick onerror onfocus

onkeydown onkeypress onkeyup onmouseover

onload onmouseup onmousedown onsubmit


##Non-standard JavaScript

In addition to reserved keywords, there are also some non-standard keywords in JavaScript implementation.

An example is the const keyword, used to define variables. Some JavaScript engines treat const as a synonym for var. Other engines treat const as a read-only variable definition.

Const is a JavaScript extension. The JavaScript engine supports its use in Firefox and Chrome. But it's not part of the JavaScript standards ES3 or ES5. Recommendation: Don't use it.


Next Section

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> function foo(obj) { with (obj) { a = 2; } } var o1 = { a: 3 }; var o2 = { b: 3 }; foo(o1); console.log(o1.a); // 2 foo(o2); console.log( o2.a ); // undefined console.log( a ); // 2 </script> </head> <body> <p>请在浏览器中打开 F12 观察</p> </body> </html>
submitReset Code
ChapterCourseware