Asked 7 years ago
7 Jan 2017
Views 895
Rasi

Rasi posted

how prototype work in JavaScript ?

how prototype work in JavaScript ? i mean its alternative of the object if no why one should use prototype in JavaScript ?


 function main(){}

 main.prototype.sub = function(){
   alert("me using prototype method.");
};

var main = new main();
  main.sub() ;


also same work with object


 function main(){
    this.sub=function (){
    alert("me using instance method.");
    };
}
var main = new main();
main.sub();


why one do use prototype and what is benefit of using .prototype ?
prototype is used to extend the functionality of object - yogi  
Jan 7 '17 06:49
Phpworker

Phpworker
answered Nov 30 '-1 00:00

You can say its late binding to object in JavaScript by prototype. . dynamically add more method or variable to object or function in JavaScript by prototype.


 function main(){
// i forget to code here
}

suppose you have main function and now you need to attach it with more variable or function . you can do it dynamically at any point of the coding in JavaScript by prototype. .

 function main(){
// i forget to code here
}
main.prototype.sub = function(){
   alert("me using prototype method.");
};


i add sub function as late binding to main function . so you can attach more and more variable and function in JavaScript by prototype.

correct me if i am wrong
Post Answer