sashomaga

Prototype

Mar 5th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The prototype object help to quickly add a custom property or method to an object that is reflected on all instances of it
  2.         //Object must be created with the new keyword
  3.         //example 1
  4.         function backwards() {
  5.             for (var i = this.length - 1; i >= 0; i--) {
  6.                 document.write(this.charAt(i));
  7.             }
  8.         }
  9.         String.prototype.write = backwards;
  10.         var test = new String("Hello man");
  11.         test.write();
  12.         //example2
  13.         function dummy() {
  14.  
  15.         }
  16.         var test = new dummy();
  17.         function myProperty(name){
  18.             this.name = name;
  19.         }
  20.         function myMethod() {
  21.             console.log("Say something");
  22.         }
  23.         dummy.prototype.name = myProperty;
  24.         dummy.prototype.method = myMethod;
  25.         test.prop = "Pesho";
  26.         console.log(test.prop);
  27.         test.method();
Advertisement
Add Comment
Please, Sign In to add comment