Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Survey of the JavaScript.docx
Скачиваний:
4
Добавлен:
18.08.2019
Размер:
50.4 Кб
Скачать

Constructor

Functions which are used to initialize objects are called constructors. The calling sequence for a constructor is slightly different than for ordinary functions. A constructor is called with the new prefix:

new Constructor(parameters...)

By convention, the name of a constructor is written with an initial capital.

The new prefix changes the meaning of the this variable. Instead of its usual value, this will be the new object. The body of the constructor function will usually initialize the object's members. The constructor will return the new object, unless explicitly overridden with the return statement.

The constructed object will contain a secret prototype link, which contains a reference to the constructor's prototype member.

Prototype

Objects contain a hidden link property. This link points to the prototype member of the constructor of the object.

When items are accessed from an object by the dot notation or the subscript notation, if the item is not found in the object then the link object is examined. If it is not found in the link object, and if the link object itself has a link object, then that link object is examined. If the chain of link objects is exhausted, then undefined is returned.

This use of prototype link chains provides a sort of inheritance.

Members can be added to the prototype by assignment. Here we define a new class Demo, which inherits from class Ancestor, and adds its own method foo.

function Demo() {}

Demo.prototype = new Ancestor();

Demo.prototype.foo = function () {};

Vars

Named variables are defined with the var statement. When used inside of a function, var defines variables with function-scope. The vars are not accessible from outside of the function. There is no other granularity of scope in JavaScript. In particular, there is no block-scope.

Any variables used in a function which are not explicitly defined as var are assumed to belong to an outer scope, possibly to the Global Object.

Vars which are not explicitly initialized are given the value undefined.

Vars are not typed. A var can contain a reference to an object, or a string or a number or a boolean or null or undefined.

A new set of vars is made every time the function is called. This allows functions to be recursive.

Closure

Functions can be defined inside of other functions. The inner function has access to the vars and parameters of the outer function. If a reference to an inner function survives (for example, as a callback function), the outer function's vars also survive.

Return

JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.

Statements

The set of named statements includes var, if, switch, for, while, do, break, continue, return, try, throw, and with. Most of them work the same as in other C-like languages.

The var statement is a list of one or more variables names, separated by commas, with optional initialization expressions.

var a, b = window.document.body;

If the var statement appears outside of any function, it adds members to the Global Object. If it appears inside of a function, it defines local variables of the function.

In if statements, while statements, do statements, and logical operators, JavaScript treats false, null, undefined, "" (the empty string), and the number 0 as false. All other values are treated as true.

The case labels in a switch statement can be expressions. They don't have to be constants. They can be strings.

There are two forms of the for statement. The first is the common (inittestinc) form. The second is an object iterator.

for (name in object) {

if (object.hasOwnProperty(name)) {

value = object[name];

}

}

The block is executed for each name in the object. The order in which the names are produced is not guaranteed.

Statements can have a label prefix, which is an identifier followed with a colon.

The with statement should not be used.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]