HTML and CSS Reference
In-Depth Information
"I am an object"
In this case you have not modified the implementation of toString on the prototype; it has
been overridden on this specific instance. In order to prove this, you can create a new empty
object, and execute the toString method on it:
> obj2 = {}
> obj2.toString()
"[object Object]"
The properties on a prototype are immutable, just like strings are immutable. Although all
objects with the same prototype share the properties on the prototype, they cannot change
the prototype; they simply override these properties on themselves.
The specific prototype assigned to objects created with the object literal notation is the Ob-
ject.prototype . Different objects can however have different prototypes. For instance, if
we create an array using the array literal notation, the variable is still an object:
> a = [1,2,3,4,5];
> typeof a
"object"
Because this is an object, it will contain the Object prototype somewhere in its hierarchy,
and therefore we can invoke the toString method. You will notice that the toString imple-
mentation has been specially tailored for arrays:
> a.toString()
"1,2,3,4,5"
The array instance also has access to a whole set of other properties that were not available
to our object created with the object literal notation:
> a.reverse()
[5, 4, 3, 2, 1]
Search WWH ::




Custom Search