Literal (computer programming)

Source: Wikipedia, the free encyclopedia.

In

arrays, records, and objects. An anonymous function is a literal for the function type
.

In contrast to literals,

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, in the following, 1 is an integer literal and the three letter string in "cat" is a string literal:

int a = 1;
string s = "cat";

In

keywords
, like true for the Boolean literal "true".

In some

function literals
. The brace notation below, which is also used for array literals, is typical for object literals:

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

Literals of objects

In ECMAScript (as well as its implementations JavaScript or ActionScript), an object with methods can be written using the object literal like this:

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

These object literals are similar to

anonymous classes in other languages like Java
.

The JSON data interchange format is based on a subset of the JavaScript object literal syntax, with some additional restrictions (among them requiring all keys to be quoted, and disallowing functions and everything else except data literals). Because of this, almost every valid JSON document (except for some subtleties with escaping) is also valid JavaScript code, a fact exploited in the JSONP technique.

See also

References

  1. OCLC 298763
    .
  2. ^ "Literals". IBM Knowledge Center. 18 June 2009. Retrieved 13 May 2020.