prototype

The prototype property is used to allow the addition of properties or methods to all instances of the Number object with which it is used.

syntax:

Number.prototype.property

Number.prototype.method

EXAMPLE

var globalVariableOne = new Number();

function functionOne(thingy) {

localVariableOne = (thingy * 5);

return localVariableOne;

}

Number.prototype.quintupled = functionOne;

document.write("The value, now quintupled, is " + globalVariableOne.quintupled(200) );

The example is quite complex, but was required to show the use of the prototype property. An instance of the number object was first created, and was called globalVariableOne. It has no value as of right now. A function was then created, called functionOne. Within functionOne is one local variable, localVariableOne. localVariableOne is loaded with the contents of "thingy" multiplied by five. So far, what we've accomplished is the declaration of a Number, which will be multiplied by five.



Next comes the use of the prototype property. A property called "quintupled" is assigned to it. The function name after the equals sign is the "functionOne" function. We have now assigned a property called "quintupled" to the globalVariableOne Number, which will be multiplied by five, as indicated within functionOne.



The document.write statement is then used to display the words "The value, now quintupled, is ", then the variable globalVariableOne.quintupled, which is a Number multiplied by five. The number to be multiplied by five is included within the brackets after the property name "quintupled". The result, which is 1000, is then written to the screen.