↑
Main Page
Operators
Here, the code asks, “Is variable
s
an instance of the class
String
?” Yes it is, so the result is
“true”
.
Although not as versatile as
typeof
,
instanceof
does offer enough help for the cases when
typeof
returns
“object”
.
Operators
ECMA-262 describes a set of
operators
that can be used to manipulate variables. The operators range
from mathematical operators (such as addition and subtraction) and bitwise operators to relational
operators and equality operators. Any time a native action is performed on a value, it is considered
an operator.
Unary operators
Unary operators take only one parameter: the object or value to operate on. They are the simplest opera-
tors in ECMAScript.
delete
The
delete
operator erases a reference to an object property or method that was previously defined.
Example:
var o = new Object;
o.name = “Nicholas”;
alert(o.name); //outputs “Nicholas”
delete o.name;
alert(o.name); //outputs “undefined”
In this example, the
name
property is deleted, which means that it is forcibly de-referenced and set
to
undefined
(which you will remember is the same value a variable has when it is created and not
initialized).
The
delete
operator
cannot
be used to delete properties and methods that are not defined by the devel-
oper. For instance, the following line causes an error:
delete o.toString;
Even though
toString
is a valid name of a method, this code line causes an error because the
toString()
method is native to ECMAScript and not developer-defined.
void
The
void
operator returns
undefined
for any value. This is typically used to avoid outputting a value
that shouldn’t be output, such as when calling a JavaScript function from an HTML
<a>
element. To do
this properly, the function cannot return a valid value; otherwise the browser erases the page and dis-
plays only the result of the function. For example:
<a href=”javascript:window.open(‘about:blank’)”>Click Me</a>
33
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 33
Free JavaScript Editor
Ajax Editor
©
→ R7