Page 2 of 3
Accessing properties and methods
Of course as a method is just a property that happens to be a function we really don't need to deal with methods as a separate case - but it helps to see what the syntax looks like.
To access properties and methods you can use the usual dot notation:
or an array index notation:
The notation you use makes no difference to the way the property or method is accessed.
For example you can write
they both mean and result in exactly the same thing.
There is another subtle point that is worth keeping in mind. The object that has been created is not myObject.
The myObject variable simply references (or points to) the anonymous object that has been created.
To illustrate this idea we can simply create another variable that references the same object:
now both myObject and yourObject reference the same anonymous object i.e. there is, still, only one object.
You can easily prove this fact because changing a property using one of the variables changes the property when accessed by the other variable.
Now ask yourself the question of what the name of the object is? It clearly doesn't have a name even though when an object is created with a single variable to reference it we usually allow the confusion between the variable's name and the object. So we say that the object instance is called myObject. This of course breaks down as soon as we have an object referenced by two or more variables. This is the sense in which objects that are referenced by variables are anonymous.
You can take this idea to simply mean you have to be careful when assigning objects to variables but it is deeper than this. The object you create has an existence independent of the variables that reference it.
Context and this
Now consider the example given earlier.
The object created has a single property myProperty with a value of 123. It has a single method myMethod which is set to reference a function which simply returns the value of myProperty.
Notice that the function is defined in the usual way and can have as many lines of code as it takes.
The only oddity is the way that the property myProperty has to be referred to by its full name myObject.myProperty.
Notice that once again you really should think of this as specifying the myProperty of the object that the myObject variable references.
Usually when you are defining an object, other languages automatically provide an object context for the definitions. That is, in most other languages a reference to myProperty within myObject would be interpreted to mean myObject.myProperty.
In a prototype-object language this doesn't make much sense as you already have a reference to the instance of the class - it is stored in the myObject variable. Given the fact that you actually know the instance "name" i.e. myObject you might as well use it and be explicit about the nature of the variable you are creating.
However, this all said, you can use a shortcut to refer to the current object when writing methods. The this keyword always refers to the context that the function is being defined in and this is true even in the case of an object literal - even if this syntax is not used much in practice.
For example: you can define the object using:
var myObject =
This is often explained by saying that this is set to myObject in the object's definition and you can think of it as a sort of shorthand if it helps - but by now you should be getting the idea that it actually references the same anonymous object that myObject references.
That is you can imagine that
is executed behind the scenes as the object literal is first created.
However once outside of the curly brackets this is no longer defined in this way.
The rule is, and it's a rule that is difficult to state but easy to follow -
any variables you define within methods are in the same context as if they were defined in the same place in the program but not within an object.
only properties have object context.
For example, if the method is defined as:
then myGlobal is a variable defined elsewhere in the program and not within the object.
If the method is defined as:
return i ;
then i is a local variable to the function myMethod and is used as such. It isn't accessible outside of myMethod and this includes other methods within myObject.
In both cases the context of the variables isn't changed by being defined within an object.
You simply create an object.