Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Literal (computer programming)

From Wikipedia, the free encyclopedia
Notation for representing a fixed value in source code

Incomputer science, aliteral is a textual representation (notation) of avalue as it is written insource code.[1][2] Almost all programming languages have notations for atomic values such asintegers,floating-point numbers, andstrings, and usually forBooleans andcharacters; some also have notations forelements of enumerated types and compound values such asarrays,records, andobjects. Ananonymous function is a literal for thefunction type.

In contrast to literals,variables orconstants 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:

inta=1;strings="cat";

Inlexical analysis, literals of a given type are generally a token type, with a grammar rule, like "astring of digits" for an integer literal. Some literals are specifickeywords, liketrue for the Boolean literal "true".

In someobject-oriented languages (likeECMAScript), 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}

Literals of objects

[edit]

InECMAScript (as well as its implementationsJavaScript orActionScript), an object with methods can be written using the object literal like this:

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

These object literals are similar toanonymous classes in other languages likeJava.

TheJSON 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 theJSONP technique.

See also

[edit]

References

[edit]
  1. ^Donovan, John (1972).Systems programming. McGraw-Hill. p. 45.ISBN 978-0-07-017603-4.OCLC 298763.
  2. ^"Literals".IBM Knowledge Center. 18 June 2009. Retrieved13 May 2020.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Literal_(computer_programming)&oldid=1236326363"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp