JavaScript prototyping shortest explanation

  1.  
  2. // Without prototyping
  3.  
  4. function Foo() {
  5. this.do = function(){
  6. }
  7. }
  8.  
  9. var f1 = new Foo()
  10. var f2 = new Foo()
  11. f1.do == f2.do // FALSE
  12.  
  13. // With prototyping:
  14.  
  15. function Bar(){
  16. }
  17.  
  18. Bar.prototype.do = function(){
  19. }
  20.  
  21. var b1 = new Bar()
  22. var b2 = new Bar()
  23. b1.do == b2.do // TRUE
  24.  

No Comments yet

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.