prototype
• A prototype is a property of every JavaScript object
• Functions and properties can be associated with a
constructor's property
• When a function is invoked with new keyword, all
properties and methods of the prototype for the
function are attached to the resulting object46
// Constructor of the MyObject
function MyObject(name, size){
this.name=name;
this.size=size;
}
// Add a function to the prototype
MyObject.prototype.tellSize=function{
alert(“size of “ + this.name+” is “ + this.size);
}
// Create an instance of the object
var myObj=new MyObject(“Sang”, “30 inches”);
myObj.tellSize();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment