Java Reference
In-Depth Information
The Object Constructor Function
As we saw in the last example, all objects inherit from the Object() constructor function's
prototype object.
When an object calls a method, the JavaScript engine will check to see if the object has that
method. If it doesn't, it will check if the object's prototype has the method. If not, it will
check whether the prototype's prototype has it. This continues until it reaches the Object
constructor function's prototype, from which all objects in JavaScript inherit. If the Object
prototype is without the method, an error will be returned saying the object doesn't exist:
<< Error: "window.unicorn is not a function"
But in the prototype chain example, the Object prototype was displayed as an empty ob-
ject, so it has no methods—right? Er, actually, that's not the case.
The Object prototype object actually has a large number of methods that are inherited by
all objects. The reason why the prototype appears as an empty object literal is because all of
its methods are not enumerable.
Enumerable Properties
Properties that are not enumerable will not show up if you use a for-in loop to loop
through an object's properties and methods. There is a method called propertyIsEnu-
merable() that every object has (because it's a method of the Object prototype) that
can be used to check if a property is enumerable. We can see in the following example that
the eat() method we created earlier is enumerable (in fact, all properties and methods that
are created by assignment are enumerable):
<< true
All objects inherit a toString() method from the Object prototype, but as it's not enu-
merable, it won't show up in any objects:
Search WWH ::

Custom Search