HTML and CSS Reference
In-Depth Information
> obj.increaseAge = function() {
this.age++;
}
//
Functions inside objects are referred to as methods. They are exactly the same
as functions, except in the way they treat an important variable called this . You
may have also noticed the use of this in the method above: this will be explained
in full below.
If you now look at the object in the JavaScript console you will see that it has a set of prop-
erties with values assigned:
> obj
Object {firstName: "John", lastName: "Smith", age: 32, increaseAge: function}
The reason this is possible is because objects in JavaScript are really just associative arrays
(also known as hash maps in other languages). Associative arrays are supported natively in
most programming languages, and comprise a collection of name/value pairs.
In order to access a property on a JavaScript object, simply use the following notation:
> obj.firstName
"John"
JavaScript supports an alternative syntax for accessing and setting properties that is even
more evocative of associative arrays in other languages.
> obj['firstName']
"John"
 
Search WWH ::




Custom Search