- Object literal
In
computer science , aliteral is a notation for representing a fixed value in source code, egstring literal . In contrast to this,variables or constants are symbols that can take on one of a class of fixed values, the constant being constrained not to change. Literals are often used to initialize variables, for example:int a=1; String s="cat";
Technically, a literal will be assigned a value at
compile time , while a variable or constant will be assigned atruntime .In some object-oriented languages (like
ECMAScript ), objects can also be represented by literals. Methods of this object can be specified in the object literal usingfunction literals . The brace notation below, which is also used for array literals, is typical for object literals:{"cat","dog"} {name:"cat",length:57}
In ECMAScript/JavaScript
In
ECMAScript (as well as its derivativesJavaScript andActionScript ), an object with methods can be written using the object literal like this:To briefly state the difference from normal class definition syntax, the
keyword "class" is absent and thesemicolon used in languages likeC++ and C# is replaced by thecomma .These object literals are similar to
anonymous class es in other languages like Java.
Wikimedia Foundation. 2010.