Object literal

Object literal

In computer science, a literal is a notation for representing a fixed value in source code, eg string 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 at runtime.

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 using function 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 derivatives JavaScript and 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();

To briefly state the difference from normal class definition syntax, the keyword "class" is absent and the semicolon used in languages like C++ and C# is replaced by the comma.

These object literals are similar to anonymous classes in other languages like Java.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Literal — may refer to:*Literal and figurative language, taken in a non figurative sense. *Literal translation, the close adherence to the forms of a source language text. *Terminal symbol in regular expressions and in descriptions of formal grammars.… …   Wikipedia

  • Literal (computer programming) — In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating point numbers, strings, and booleans; some also have… …   Wikipedia

  • Literal pool — In computer science, and specifically in compiler and assembler design, a literal pool is a lookup table used to hold literals during assembly and execution.Multiple (local) literal pools are typically used only for computer architectures that… …   Wikipedia

  • Object database — Example of an object oriented model.[1] An object database (also object oriented database management system) is a database management system in which information is represented in the form of objects as used in object oriented programming. Object …   Wikipedia

  • Object Linking and Embedding — Una tabla creada en Calc embebida en un documento de Writer como un objeto OLE. Object Linking and Embedding (OLE) cuya traducción literal es incrustación y enlazado de objetos es el nombre de un sistema de objeto distribuido y un protocolo… …   Wikipedia Español

  • Function object — A function object, also called a functor or functional, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax.Function objects are unrelated to functors in… …   Wikipedia

  • Subject Object Verb — In linguistic typology, Subject Object Verb (SOV) is the type of languages in which the subject, object, and verb of a sentence appear or usually appear in that order. If English were SOV, then Sam oranges ate would be an ordinary sentence.… …   Wikipedia

  • First-class object — In computing, a first class object (also value, entity, and citizen), in the context of a particular programming language, is an entity which can be used in programs without restriction (when compared to other kinds of objects in the same… …   Wikipedia

  • JavaScript syntax — This article is part of the JavaScript series. JavaScript JavaScript syntax JavaScript topics This box: view · …   Wikipedia

  • Comparison of programming languages (mapping) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”