jQuery in Action

JavaScript Object fundamentals
325
By the way, we can also express arrays in
JSON
by placing the comma-
delimited list of elements within square brackets as in the following:
var someValues = [2,3,5,7,11,13,17,19,23,29,31,37];
As we've seen in the examples presented in this section, object reference are fre-
quently stored in variables or in properties of other objects. Let's take a look at a
special case of the latter scenario.
A.1.4 Objects as window properties
Up to this point, we've seen two ways to store a reference to a JavaScript object:
variables and properties. These two means of storing references use differing
notation, as shown in the following snippet:
var aVariable =
'Before I teamed up with you, I led quite a normal life.';
someObject.aProperty =
'You move that line as you see fit for yourself.';
These two statements each assign a
String
instance (created via literals) to a vari-
able and an object property respectively using assignment operations. (Kudos to
you if you can identify the source of the obscure quotes; no cheating with Google!
There was a clue earlier in the chapter.)
But are these statements really performing different operations? As it turns
out, they're not!
When the
var
keyword is used at the top level, outside the body of any contain-
ing function, it's only a programmer-friendly notation for referencing a property
of the pre-defined JavaScript
window
object. Any reference made in top-level
scope is implicitly made on the
window
instance.
This means that all of the following statements are equivalent:
var foo = bar;
and
window.foo = bar;
and
foo = bar;
Regardless of which notation is used, a
window
property named
foo
is created (if
it's not already in existence) and assigned the value of
bar
. Also, note that because
bar
is unqualified, it's assumed to be a property on
window
.


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.