jQuery in Action

322
APPENDIX
JavaScript that you need to know but might not!
From this example, we've learned that an instance of the JavaScript
Object
, which
we'll simply refer to as an object from here forward, is a collection of properties, each
of which consists of a name and a value. The name of a property is a string, and the
value can be any JavaScript object, be it a
Number
,
String
,
Date
,
Array
, basic
Object
, or any other JavaScript object type (including, as we shall see, functions).
This makes the primary purpose of an
Object
instance to serve as a container
for a named collection of other objects. This may remind you of concepts in other
languages: a Java map for example, or dictionaries or hashes in other languages.
When referencing properties, we can chain references to properties of objects
serving as the properties of a parent object. Let's say that we add a new property
to our
ride
instance that captures the owner of the vehicle. This property is
another JavaScript object that contains properties such as the name and occupa-
tion of the owner:
var owner = new Object();
owner.name = 'Spike Spiegel';
owner.occupation = 'bounty hunter';
ride.owner = owner;
To access the nested property, we write
var ownerName = ride.owner.name;
There are no limits to the nesting levels we can employ (except the limits of good
sense). When finished--up to this point--our object hierarchy looks as shown in
figure A.1.
Note how each value is a distinct instance of a JavaScript type.
Figure A.1 Our object hierarchy shows that Objects are containers for named references to other
Objects or JavaScript built-in objects.


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

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