This section provides a quick description of built-in default properties and methods in 'Object.prototype'. A tutorial example is provided to dump what's in 'Object.prototype'.
As I mentioned in the previous section, the default prototype object is created from the "Object()" constructor function.
Its prototype object, "Object.prototype", is the base prototype object of all other prototype objects.
Therefor, properties of "Object.prototype" will be inherited by all objects.
Because of this nature, JavaScript provides a number built-in properties and methods in "Object.prototype"
accessible by all objects through the inheritance tree. Some of them are listed below:
"constructor" - Property pointing back to the constructor function of this object.
"hasOwnProperty(prop)" - Method returning true if the specified property belongs this object,
not inherited from its prototype object chain.
"isPrototypeOf(obj)" - Method returning true if this object is one of the parent prototype objects of the specified child object.
"toString()" - Method returning the string representation of this object.
"valueOf()" - Method returning the primitive value represented by this object.
Let's first confirm that those properties and method do exist in "Object.prototype".
To do this I wrote the following tutorial example to print out their values.
Notice that how I use the hasOwnProperty() method to separate
<html>
<!-- Inheritance_Built_in_Propertie_and_Method.html
Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head>
<title>Built-in Properties and Methods in Object.prototype</title>
</head>
<body>
<pre>
<script type="text/javascript">
// Adding a property to the base prototype
Object.prototype.copyright = "herongyang.com";
// Dumping some built-in properties
document.writeln("\nObject.prototype's built-in properties:");
dumpProperty(Object.prototype, "constructor");
dumpProperty(Object.prototype, "hasOwnProperty");
dumpProperty(Object.prototype, "isPrototypeOf");
dumpProperty(Object.prototype, "toString");
dumpProperty(Object.prototype, "valueOf");
// Dumping the user added property
document.writeln("\nObject.prototype's user added properties:");
dumpProperty(Object.prototype, "copyright");
function dumpProperty(object, property) {
var inheritance;
if (object.hasOwnProperty(property))
inheritance = "Local";
else
inheritance = "Inherited";
document.writeln(" "+property+": "+inheritance+": "
+object[property]);
}
</script>
</pre>
</body>
</html>
The output of this tutorial example tells us that those properties and methods do exists and local to the "Object.prototype" object.
However, JavaScript does not want to show us their values, the definitions of functions in all cases.
Object.prototype's built-in properties:
constructor: Local: function Object() {
[native code]
}
hasOwnProperty: Local: function hasOwnProperty() {
[native code]
}
isPrototypeOf: Local: function isPrototypeOf() {
[native code]
}
toString: Local: function toString() {
[native code]
}
valueOf: Local: function valueOf() {
[native code]
}
Object.prototype's user added properties:
copyright: Local: herongyang.com
Now I am ready to update my diagram on JavaScript object inheritance model: