Posts Tagged ‘Scripting’

JavaScript Reserved Words

December 19, 2011

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
Advertisements

Function Declarations vs Function Expressions

August 17, 2011

This short post is in reaction to this post
on JavaScript Function Declarations and Function Expressions.
My concern was with the littering of the global object.

Subject to hoisting

  • function statements or function declarations
  • variable declarations, but not their assignment expressions

Function Statement
sometimes just a pseudonym for a Function Declaration
in Mozilla a Function Statement is an extension of Function Declaration
allowing the Function Declaration syntax to be used anywhere a statement
——is allowed.

Don’t forget; when a function is not the property of an object, it’s invoked as a function.
The this of the function is bound to the global object.
∴ foo and also bar are bound to the global object.
This is not good.
As Douglas Crockford puts it (and rightfully so), we’re clobbering the global variables.
I think a better way to do this, would be like the following code examples.

A few words on access first

All members of an object are public
You can add public members in the constructor, by using the
this keyword (which by the way, provides “privileged” access).
If you want to add private members, you can use a constructor Function.
You are actually adding the member to the function.
Private members are only accessible via the objects privileged members.
Privileged members are able to access private members,
and are themselves accessible to public members and outside.

Have a look at Angus Croll’s post to provide some context.

Question 1

function Foo(){
   function bar() {      //private
      return 3;
   }
   this.barMeth = bar(); //privileged
   function bar() {      //private
      return 8;
   }
}
var myFoo = new Foo();
alert(myFoo.barMeth);

Question 2

function Foo(){
   var bar = function bar() { //private
      return 3;
   }
   this.barMeth = bar();      //privileged
   var bar = function bar() { //private
      return 8;
   }
}
var myFoo = new Foo();
alert(myFoo.barMeth);

Question 3

var myFoo = new Foo();
alert(myFoo.barMeth);
function Foo(){
   var bar = function bar() { //private
      return 3;
   };
   this.barMeth = bar();      //privileged
   var bar = function bar() { //private
      return 8;
   };
}

Question 4

function Foo(){
   this.barMeth = bar();          //privileged
   var bar = function bar() {     //private
      return 3;
   };
   var barMeth = function bar() { //private
      return 8;
   };
}
var myFoo = new Foo();
alert(myFoo.barMeth);

Or worse, but still not clobbering the global object…

var myApp = {};

myApp.fooTest = {
   foo: function (){
      function foo(){
         function bar() {
            return 3;
         }
         return bar();
         function bar() {
            return 8;
         }
      }
      alert(foo());
   }
}

myApp.fooTest.foo();