My recent posts about basics or overview of something
mostly cite select matirials of sources listed in the Resources section of every
post. It only serves for personal study and learning. And if you like, you can
take any part or all of them as desired. It would be my pleasure.
Overview
Javascript is an html scripting language. In the official specification it iscalled ECMAScript.
Built-in Features
Datatypes and Values
All numbers in JavaScript are represented as 64-bit floating-point values(i.e., similar to double in java and C++).
Conversion between Strings and Numbers can be done in several ways in both
direction. Numbers are automatically converted to strings when needed, so are
strings converted to numbers.
Numbers to strings:
var n = 100;
var s = n + " bottles of beer.";
var n_as_string = n + "";
var string_value = String(number);
string_value = number.toString();
Strings to numbers:
var product = "21" * "2"; // get number 42
var number = string_value - 0;
(Note: adding zero to a string value
results in string concatenation)
var number = Number(string_value);
// And parseInt(), parseFloat.
In JavaScript, functions are values that can be manipulated
by JavaScript code. It means that functions can be stored in variables, arrays,
and objects, and it means that functions can be passed as arguments to other
functions.
Functions can be defined in three ways:
function square(x) { return x*x;}
var square = function(x) { return x*x; }
// function name here is
optional.
var square = new Function("x", "return x*x");
// awkward, less useful and
less efficient.
An object is a collection of named values. These named values are usually
referred to as properties of the object. Properties of objects are, in
many ways, just like JavaScript variables; they can contain any type of data,
including arrays, functions, and other objects. Objects in JavaScript can serve
as associative arrays (recall the same concept in Delphi/Pascal, if you
know that language); that is, they can associate arbitrary data values with
arbitrary strings.
image.width
image.height
image["width"]
image["height"]
Arrays may contain any type of JavaScript data, including references to other
arrays or to objects or functions. Also note that
JavaScript does not support multidimensional arrays,
except as arrays of arrays. Finally, because JavaScript is an untyped language,
the elements of an array do not all need to be
of the same type, as they do in typed languages like Java.
A corresponding object class is defined for each of the three key
primitive datatypes. That is, besides supporting the number, string,
and boolean datatypes, JavaScript also supports Number, String, and Boolean
classes. JavaScript can flexibly convert values from one type to another. When
you use a string in an object contexti.e., when you try to access a property or
method of the string, JavaScript internally creates a String wrapper
object for the string value. Note that the String object created when
you use a string in an object context is a transient one.
Primitive types are manipulated by value, and reference types, as the
name suggests, are manipulated by reference. Numbers and booleans are
easily manipulated at the low levels of the JavaScript interpreter. Objects, on
the other hand, are reference types. Arrays and functions, which are specialized
types of objects, are therefore also reference types.
Since strings (primitive type, not the wrapper) are immutable in JavaScript,
there is no way to tell whether strings are passed by value or by reference.
Variables
There's no fundamental difference in JavaScript between variables andthe properties of objects.
When the JavaScript interpreter starts up, one of the first things it
does, before executing any JavaScript code, is create a global
object. The properties of this object are the
global variables of JavaScript programs. When you declare a global JavaScript
variable, what you are actually doing is defining a property of the global
object.
The JavaScript interpreter initializes the global object with a number of
properties that refer to predefined values and functions. For example, the
Infinity, parseInt, and Math properties refer to the
number infinity, the predefined parseInt( ) function, and the
predefined Math object, respectively.
In top-level code (i.e., JavaScript code that is not part of a function), you
can use the JavaScript keyword this to refer to the global
object.
In client-side JavaScript, the Window object
serves as the global object for all JavaScript code contained in the
browser window it represents. This global Window object has a self-referential
window property that can be used instead of this to refer to
the global object. The Window object defines the core global properties, such as
parseInt and Math, and also global client-side properties,
such as navigator and screen.
For local variables, while the body of a function is executing, the function
arguments and local variables are stored as properties of another special
object. This object is known as the call object.
Each time the JavaScript interpreter begins to execute a function, it creates
a new execution context for that function. Thus,
JavaScript code that is not part of any function runs in an execution context
that uses the global object for variable definitions. A JavaScript
implementation may allow multiple "global" execution contexts. The
obvious example is client-side JavaScript, in which each separate browser
window, or each frame within a window, defines a separate global execution
context.
Object Support
ECMAScript does not contain proper classes such as those in C++, Smalltalk,or Java. An ECMAScript object is an unordered collection of properties each with
zero or more attributes.
It turns out that every JavaScript object includes an internal
reference to another object, known as its prototype object. All
functions have a prototype property that is automatically created and
initialized when the function is defined. The initial value of the
prototype property is an object with a single property. This property
is named constructor and refers back to the constructor function with
which the prototype is associated.
Property inheritance occurs only when you read property values, not
when you write them. If you set the property p in an
object o that inherits that property from its prototype, what
happens is that you create a new property p directly in
o. Now that o has its own property named
p, it no longer inherits the value of p from
its prototype.
Navigator Object
The JavaScriptnavigator object is the object representation of the client internet browser
or web navigator program that is being used. This object is the top level object
to all others.
DOM Object
Overview
The goal of the DOM group is to define a programmatic interface for XML andHTML. It is platform- and language-neutral interface. The DOM is separated into
three parts: Core, HTML, and XML. The Core DOM provides a low-level set of
objects that can represent any structured document.
DOM is being designed at several levels:
- "Level 1. This concentrates on the actual core, HTML, and XML document
models. It contains functionality for document navigation and manipulation. - Level 2. Includes a style sheet object model, and defines functionality for
manipulating the style information attached to a document. It also enables
traversals on the document, defines an event model and provides support for XML
namespaces. - Level 3. Will address document loading and saving, as well as content models
(such as DTDs and schemas) with document validation support. In addition, it
will also address document views and formatting, key events and event groups.
First public working drafts are available. - Further Levels. These may specify some interface with the possibly
underlying window system, including some ways to prompt the user. They may also
contain a query language interface, and address multithreading and
synchronization, security, and repository."
Resources
- ECMAScript
Language Specification 3rd edition - Ajax
in Action - The
CTDP JavaScript Manual Version 0.6.0, December 31, 2000 - W3C Document Object Model
(DOM) - DOM
objects and methods - JavaScript - The Definitive Guide, 5th Edition
This is a rough draft and published temporarily.

No comments:
Post a Comment