Is "Clear" a Reserved Word in JavaScript

Is clear a reserved word in Javascript?

As the others said, clear is not a reserved keyword. It seems that the called function is document.clear [MDN]. Invoking

console.log(clear === document.clear);

inside the event handler returns true.

DEMO

So it seems, document is in the scope chain of the event handler.... the question now is why.

JavaScript: The Definitive Guide says:

In an event handler as HTML attribute, the Document object is in the scope chain before the Window object (...)

As your method is global, meaning it is a property of the window object, it is not found in the scope chain, as document.clear comes earlier in the scope chain.

I haven't found any specification for this. The guide also says that one should not rely on that, so I assume this is nothing official.

If you have form elements inside a form, then even the corresponding form element will be in the scope chain (not sure whether this holds for all browsers though). This is another reason for confusion.


There are two (not exclusive) ways to avoid such situations:

  • Don't use inline event handlers. It is considered bad practice as it is mixing logic and presentation. There are other ways to attach event handlers.

  • Don't pollute the global namespace. Create one object in global scope (with a name you are sure of does not collide with any window or document properties or ids of HTML elements) and assign the functions as properties of this object. Whenever you call a function, you reference it through this object. There also other ways to namespace your code.

In Javascript, what does the reserved keyword short do?

They belong to the so-called future keywords by the ECMAScript specification.

They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.

short as a reserved word was part of an old ECMAScript specification:

The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3)

[...] short [...]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords

In ECMAScript 5/6 short was removed from the list of reserved words. Nonetheless for compatibility with older browsers not implementing either, you shouldn't use it anyway.

Reserved keywords in JavaScript

We should be linking to the actual sources of info, rather than just the top google hit.

http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words

JScript 8.0:
http://msdn.microsoft.com/en-us/library/ttyab5c8.aspx

Is 'err' a reserved word for an error?

No it is not... it is a completely arbitrary name that you assign yourself to a first function parameter in the arguments list

Why function with name clear doesn't work in JavaScript

The reasoning behind this is that clear() inside a button calls document.clear instead of your clear.

Javascript reserved word and object

Instead of using a JS object, you could use the built-in Map type which uses strings/symbols as keys and does not conflict with any existing properties.

Replace
var dictionary = {} with var dictionary = new Map()



Related Topics



Leave a reply



Submit