Guest User

Untitled

a guest
Oct 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. var c = {
  2. name: 'The c object',
  3. log: function(){
  4. this.name = 'Updated c object';
  5. console.log(this);
  6. var setName = function(newname){
  7. // this now points to the global object
  8. this.name = newname;
  9. };
  10. setName('Updated again! The c object');
  11. console.log(this);
  12. }
  13. }
  14. c.log();
  15.  
  16. // To fix it we insert a new variable self and give it a reference to this
  17. var d = {
  18. name: 'The c object',
  19. log: function(){
  20. var self = this;
  21. self.name = 'Updated c object';
  22. console.log(this);
  23. var setName = function(newname){
  24. // this now points to the global object
  25. self.name = newname;
  26. };
  27. setName('Updated again! The c object');
  28. console.log(self);
  29. }
  30. }
  31. d.log();
Add Comment
Please, Sign In to add comment