↑
Main Page
Global
When the interpreter finds an
eval()
call, it interprets the argument into actual ECMAScript statements
and then inserts it into place. This means that variables can be referenced inside of an
eval()
call that is
defined outside of its argument:
var msg = “hello world”;
eval(“alert(msg)”);
Here, the variable
msg
is defined outside of the context of the
eval()
call, yet the alert is still displayed
with the text
“hello world”
because the second line is replaced with a real line of code. Likewise, you
can define a function or variables inside of an
eval()
call that can be referenced by the code outside of
itself:
eval(“function sayHi() { alert(‘hi’); }”);
sayHi();
Here, the
sayHi()
function is defined inside of an
eval()
call. Because that call is replaced with the
actual function, it is possible to call
sayHi()
on the following line.
The
Global
object doesn’t just have methods, it also has properties. Remember those special values
undefined
,
NaN
, and
Infinity
? They are all properties of the
Global
object. Additionally, all native
object constructors are also properties of the
Global
object. The following table describes all the proper-
ties in more detail.
Property
Description
undefined
The literal for the Undefined type.
NaN
The special Number value for
Not a Number
.
Infinity
The special Number value for an infinite value.
Object
Constructor for
Object
.
Array
Constructor for
Array
.
Function
Constructor for
Function
.
Boolean
Constructor for
Boolean
.
String
Constructor for
String
.
Number
Constructor for
Number
.
Date
Constructor for
Date
.
RegExp
Constructor for
RegExp
.
Table continued on following page
This capability is very powerful, but also very dangerous. Use extreme caution with
eval()
, especially when passing user-entered data into it. A mischievous user could
insert values that could compromise your site or application security (this is called
code injection).
83
Object Basics
06_579088 ch03.qxd 3/28/05 11:36 AM Page 83
Free JavaScript Editor
Ajax Editor
©
→ R7