Javascript object common properties

Add common properties for object instances.

The prototype object.

All constructor function type objects, such as the build-in objects Object, Function, Number, String, Date or
other that you creates on your own with the Function object have a prototype property which
is a reference to an object that contain common properties for
all instantiated objects.

All created object instances has a hidden reference to this prototype object, but you don't need to
use that as you can access the common properties directly through the object instance variable.

Create common properties for all object instances.

There are two main ways to add new common properties for all object instances:

Add a property to the prototype property using the Object constructor.

This is done using the prototype property on the constructor name:

Example:

<script type="text/javascript">
var obj ={};
// warning is a property that has a value of a function
Object.prototype.warning=function() {
return"This property can be used by all object instances.";
}
document.write("obj: "+obj.warning()+"<br>");
var num = new Number(); // inherited build-in objectdocument.write("num: "+num.warning()+"<br>");
</script>

Add a property to the prototype property using a created object instance.

A created object has a property, constructor, that refer back to the constructor name of the object.

To do it this way we use the property, prototype, on that constructor: