CoreJS.Literals

Object Literals

A literal syntax exists in JavaScript that is commonly used. The following code...

var foo = new Object();

foo.alpha = 90;

foo.bravo = function() {
   return 7;
};

...can be represented as...

var foo = {

    alpha: 90,

    bravo: function() {
       return 7;
    }
};

The whitespace is of course optional. The following is equivalent, albeit ugly:

var foo={alpha:90,bravo:function(){return 7;}};

Additionally, an empty object may be defined simply as "{}":

var foo = {};

Array Literals

Arrays may be also be represented using a literal syntax. The following two snippets of code are effectively identical.

var emptyArray = new Array();

var anArray = new Array("a", "Q", "z");
var emptyArray = [];

var anArray = ["a", "Q", "z"];

last edited 2007-10-31 01:48:47 by TodLiebeck