darkmavis1980

Untitled

Feb 23rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var test = function(){
  2.   this.name = 'Lluis';
  3.   this.age = 30;
  4. }
  5.  
  6. test.prototype = {
  7.   sayMyName: function(){
  8.     console.log(this.name);
  9.   },
  10.   toSource: function(){
  11.     return JSON.stringify(this);
  12.   }
  13. }
  14.  
  15. var obj = new test();
  16.  
  17. obj.sayMyName(); //prints Lluis
  18.  
  19. obj.name = 'Alessio';
  20. obj.age = 36;
  21. obj.country = 'Italy';
  22.  
  23. obj.sayMyName(); //prints Alessio
  24.  
  25. var str = 'Obj ' + obj.toSource();
  26. console.log(str); //prints Obj {"name":"Alessio","age":36,"country":"Italy"}
Add Comment
Please, Sign In to add comment