Funnily enough, most of these are not used in the language.
They cannot be used to name variables or parameters.
In saying that,
I did some testing below and that statement’s not entirely accurate.
Usage of keywords in red should be avoided.
Reserved | Keyword | Comments |
abstract | no | |
boolean | no | |
break | yes | |
byte | no | No type of byte in JavaScript |
case | yes | |
catch | yes | |
char | no | JavaScript doesn’t have char. Use string instead |
class | no | technically JavaScript doesn’t have class |
const | no | no const, but read-only can be implemented |
continue | yes | |
debugger | yes | |
default | yes | |
delete | yes | |
do | yes | |
double | no | JavaScript only has number (64 bit floating point) |
else | yes | |
enum | no | |
export | no | |
extends | no | |
false | yes | |
final | no | |
finally | yes | |
float | no | JavaScript only has number (64 bit floating point) |
for | yes | |
function | yes | |
goto | no | |
if | yes | |
implements | no | JavaScript uses prototypal inheritance. Reserved in strict mode |
import | no | |
in | yes | |
instanceof | yes | |
int | no | JavaScript only has number (64 bit floating point) |
interface | no | technically no interfaces, but they can be implemented. Reserved in strict mode |
let | no | Reserved in strict mode |
long | no | JavaScript only has number (64 bit floating point) |
native | no | |
new | yes | use in moderation. See comments in Responses below |
null | yes | |
package | no | Reserved in strict mode |
private | no | access is inferred. Reserved in strict mode |
protected | no | JavaScript has privileged, but it’s inferred. Reserved in strict mode |
public | no | access is inferred. Reserved in strict mode |
return | yes | |
short | no | JavaScript only has number (64 bit floating point) |
static | no | Reserved in strict mode |
super | no | |
switch | yes | |
synchronized | no | |
this | yes | |
throw | yes | |
throws | no | |
transient | no | |
true | yes | |
try | yes | |
typeof | yes | |
var | yes | |
volatile | no | |
void | yes | |
while | yes | |
with | yes | |
yeild | no | Reserved in strict mode |
–
When reserved words are used as keys in object literals,
they must be quoted.
They cannot be used with the dot notation,
so it is sometimes necessary to use the bracket notation instead.
Or better, just don’t use them for your names.
var method; // ok var class; // illegal object = {box: value}; // ok object = {case: value}; // illegal object = {'case': value}; // ok object.box = value; // ok object.case = value; // illegal object['case'] = value; // ok
–
I noticed in Doug Crockfords JavaScript The Good Parts
in Chapter 2 under Names, where he talks about reserved words.
It says:
“It is not permitted to name a variable or parameter with a reserved
word.
Worse, it is not permitted to use a reserved word as the name of an object
property in an object literal or following a dot in a refinement.”
I tested this in Chrome and FireFox with the following results.
var private = 'Oh yuk'; // if strict mode is on: Uncaught SyntaxError: Unexpected strict mode reserved word var break = 'break me'; // Uncaught SyntaxError: Unexpected token break
var myFunc = function (private, break) { // if strict mode is on or off: Uncaught SyntaxError: Unexpected token break // strangly enough, private is always fine as a parameter. }
var myObj = { private: 'dung', // no problem break: 'beetle' // no problem } console.log('myObj.private: ' + myObj.private) // myObj.private: dung console.log(' myObj.break: ' + myObj.break); // myObj.break: beetle
JavaScript also predefines a number of global variables and functions
that you should also avoid using their names for your own variables and functions.
Here’s a list:
- arguments
- Array
- Boolean
- Date
- decodeURI
- decodeURIComponent
- encodeURI
- encodeURIComponent
- Error
- eval
- EvalError
- Function
- Infinity
- isFinite
- isNaN
- JSON
- Math
- NaN
- Number
- Object
- parseFloat
- parseInt
- RangeError
- ReferenceError
- RegExp
- String
- SyntaxError
- TypeError
- undefined
- URIError
Tags: JavaScript, Scripting
July 2, 2012 at 07:22 |
Can you add clarifications to why new should be avoided?
The new keyword is actually ver good – it has a performance benefit over call and apply and direct invocations – when used with constructors and prototypal lookup you get an enormous memory savings too –
Thx
July 3, 2012 at 00:33 |
Hi David.
I’m curious as to what the performance benefit is over call and apply and direct invocations?
Can you shed some light as to the enormous memory savings your talking about?
Any time you call new, a new function is created in memory.
Unless your adding new methods to the function, there is no point in creating another function that looks the same as the previous new’d function.
If you are adding a new method, your better off to add it to the existing functions prototype.
I didn’t say it should be avoided.
I did say “use in moderation” though,
and understand what happens with it’s usage in every different case.
There are gotchas every where.
Using
new
has many behaviours, depending on where and how you use it.Easily enough to devote a large blog post to.
Which is why I’d recommend not using it.
There are other ways to reap the same benefits without the nasty unexpected behaviour.
Using
new
withObject()
creates an instance based on the parameter you passObject()
.Not always a bad thing (although some say it is), but can produce unexpected results if not careful.
Forgetting to use
new
with a constructor function makes the function object’sthis
reference the global object.If using in a browser that doesn’t support ES5’s
'use strict'
you won’t get a syntax or runtime error.If your constructor has this.myMember in it, your actually augmenting the global object with myMember.
There are ways to make this less likely.
1) Following the upper case first character rule.
2) using the that convention. Add all members to
that
, then returnthat
rather than the implicit return ofthis
.function Waffle() {
var that = {};
that.tastes = "yummy";
return that;
}
For simpler objects, you don’t even need a local variable such as that; you can simply return an object from a literal like so:
function Waffle() {
return {
tastes: "yummy"
};
}
You have the freedom to return any object in your constructors, as long
as it’s an object. Attempting to return something that’s not an object (like a string or a
boolean false, for example) will not cause an error but will simply be ignored, and the
object referenced by this will be returned instead.
Using any of the implementations above
Waffle()
always returns an object, regardlessof how it’s called:
var first = new Waffle(),
second = Waffle();
console.log(first.tastes); // "yummy"
console.log(second.tastes); // "yummy"
The problem with this pattern is that the link to the prototype is lost, so any members
you add to the
Waffle()
prototype will not be available to the objects.Inside the constructor function an object is created (which references the constructors prototype) and referenced by the this variable.
this references the new constructor object, the constructors prototype object, and any properties of the
new
‘d constructor function.With ES5 if you ‘use strict’, if you forget to use the new prefix on a constructor, an exception will be thrown, rather than silently adding to the global object.
Object.create
I think is a better option than using new.It’s simpler and produces a more predictable result.
You get the memory savings your talking about,
and it can reduce object initialisation time.
var child = Object.create(parent, {
age: { value: 2 } // ECMA5 descriptor
});
child.hasOwnProperty("age"); // true
If you can’t wait for all browsers to support it, or your coding for legacy browsers as I am, you can hand role the function.
Or use a library that has it.
In YUI3 it’s the
Y.Object()
method.Or implement something similar your self:
if (typeof Object.create !== 'function') {
Object.create = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
}
var childObject = Object.create(parentObject);