Object literal

From Wikipedia, the free encyclopedia

In Computer Science, a literal is an expression in source code which defines a fixed value. This contrasts with a variable which is a symbol that can take on one of a class of fixed values. A constant is also a symbol, but constrained not to change. Often variables are initialized with literals, eg

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 at runtime.

This concept is extended in object-oriented languages, although languages do not necessarily support representation of objects by literals. If they do, the brace notation below is typical, shown for an array and more general object.

{"cat","dog"}
{name:"cat",length:57}

[edit] Methods

var newobj = {
  var1: true,
  var2: "very interesting",
  method1: function () {
    alert(this.var1)
  },
  method2: function () {
    alert(this.var2)
  }
}
newobj.method1();
newobj.method2();