|
JavaScript 2.0
Core Language
Concepts
|
Tuesday, February 15, 2000
A value is an entity that can be stored in a variable, passed to a function, or returned from a function. Sample values include:
undefinednull5 (a number)true (a boolean)"Kilopi" (a string)[1, 5, false] (a three-element array){a:3, b:7} (an object with two properties)function (x) {return x*x} (a function)String (a class, a function, and a type)A type t represents two things:
The set S indicates which values are considered to be members of type t. We write v t to indicate that value v is a member of type t. The mapping M indicates how values may be coerced to type t. For each value v already in S, the mapping M must map v to itself.
A value can be a member of multiple sets, and, in general, a value belongs to more than one type. Thus, it is generally not useful to ask about the type of a value; one may ask instead whether a value belongs to some given type. There can also exist two different types with the same set of values but different coercion mappings.
On the other hand, a variable does have a particular type. If we declare a variable x of type t, then whatever value is held in x is guaranteed to have type t, and we can assign any value of type t to x. We may also be able to assign a value v t to x if type t's mapping specifies a coercion for value v; in this case the coerced value is stored in x.
Every type represents some set of values but not every set of values is represented by some type (this is required for logical consistency -- there are uncountably infinitely many sets of values but only countably infinitely many types).
Every type is also itself a value -- we can store a type in a variable, pass it to a function, or return it from a function.
If type a's set of values is a subset of type b's set of values, then we say that that type a is a subtype of type b. We denote this as a b.
Subtyping is transitive, so if a b and b c, then a c. Subtyping is also reflexive: a a. Also, if v t and t s, then v s.
The set of all values is represented by the type any, which is the supertype of all types. A variable with
type any can hold any value. The set of no values is represented by the type none, which is the
subtype of all types. A function with the return type none cannot return.
A class is a template for creating similar values, often called objects or instances. These instances generally share characteristics such as common methods and properties.
Every class is also a type and a value. When used as a type, a class represents the set of all possible instances of that class.
A class C can be derived from a superclass S. Class C can then inherit characteristics of class S. Every instance of C is also an instance of S, but not vice versa, which, by the definition of subtyping above, implies that C S when we consider C and S as types.
The subclass relation imposes a hierarchy relation on the set of all classes. JavaScript 2.0 currently does not support multiple inheritance, although this is a possible future direction. If multiple inheritance were allowed, the subclass relation would impose a partial order on the set of all classes.
A scope represents a region of JavaScript source code. The JavaScript statements or expressions package,
class, function, and scope{ } define scopes in the source code. The top level
of a JavaScript program is also a scope, called the global scope. A scope is a static entity that does not change while a
JavaScript program is running (except that if the program calls eval then new JavaScript source code will be
created which may share existing scopes or create its own scopes).
A scope may be contained inside another scope. If two scopes overlap, one must be contained entirely within the other,
so scopes form a hierarchy. There is a scope, called public, that encloses all other scopes, including global
scopes.
Scope information is used at run time to help with variable and property lookups and visibility checks.
A scope should not be confused with an activation frame, which is a runtime binding of local variables to values. A scope should also not be confused with a namespace, which is a binding of names to values.
A namespace maps names to values. When looking up property p of object o, the object's namespace is consulted for a binding of p. An object may have several different namespaces which are selected based on scope information (some properties of o may only be visible from the scope where o's class is defined) and whether a property is being read or written.
An activation frame contains a simple namespace that maps names of local variables to their getters and setters.
|
Waldemar Horwat Last modified Tuesday, February 15, 2000 |