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

Operators

JavaScript has a fairly large set of operators. Most of them work the same way as in other C-like languages. There are a few differences to watch out for.

The + operator is used for both addition and concatenation. If either of the operands is a string, it concatenates. This can cause errors. For example, '$' + 3 + 4 produces '$34', not'$7'.

+ can be used as a prefix operator, converting its string operand to a number.

!! can be used as a prefix operator, converting its operand to a boolean.

The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, "" (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:

var value = p && p.name; /* The name value will

only be retrieved from p if p has a value, avoiding an error. */

The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:

value = v || 10; /* Use the value of v, but if v

doesn't have a value, use 10 instead. */

JavaScript supplies a set of bitwise and shift operators, but does not have an Integer type to apply them to. What happens is the Number operand (a 64-bit floating-point number) is converted to a 32-bit integer before the operation, and then converted back to floating point after the operation.

In JavaScript, void is a prefix operator, not a type. It always returns undefined. This has very little value. I only mention it in case you accidently type void out of habit and are puzzled by the strange behavior.

The typeof operator returns a string based on the type of its operand.

Mistakes were made.

Object

'object'

Array

'object'

Function

'function'

String

'string'

Number

'number'

Boolean

'boolean'

null

'object'

undefined

'undefined'

Potpourri

Global Object

The Global Object is the keeper of all of the functions and variables which were not defined inside of other functions and objects. Surprisingly, the Global Object does not have an explicit name in the language. Sometimes the this variable points at it, but often not. In the web browsers, window and self are members of the Global Object which point to the Global Object, thus giving an indirect way of addressing it.

If a variable is accessed, but is not found in the current scope, it is looked for in the Global Object. If it is not found there, an error will result.

The ECMAScript specification does not talk about the possibility of multiple Global Objects, or contexts, but browsers support this. Each window has its own Global Object.

Semicolon Insertion

One of the mistakes in the language is semicolon insertion. This is a technique for making semicolons optional as statement terminators. It is reasonable for IDEs and shell programs to do semicolon insertion. It is not reasonable for the language definition to require compilers to do it. Use semicolons.

Reserved Words

JavaScript is very heavy handed in its restrictions on reserved words. The reserved words are

abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while with

Most of those words are not even used in the language. A reserved word cannot be used

  1. As a name in literal object notation

  2. As a member name in dot notation

  3. As a function argument

  4. As a var

  5. As an unqualified global variable

  6. As a statement label

There is no excuse for the first two restrictions. None. There is an excuse for the second two restrictions, but it is very weak.

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