Guest User

Untitled

a guest
May 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. // Example 1
  2. var counter = {
  3. val: 0, // property 'val'
  4. increment: function() {
  5. // method 'increment' in object 'counter'
  6. this.val += 1; // counter.val += 1;
  7. }
  8. };
  9. counter.increment();
  10. console.log(counter.val); // 1
  11. counter['increment']();
  12. console.log(counter.val); // 2
  13.  
  14.  
  15. // Example 2
  16. var obj = {
  17. fn: function(a,b) {
  18. // method 'fn' in object 'obj'
  19. return this;
  20. }
  21. };
  22. var obj2 = {
  23. method: obj.fn
  24. };
  25. console.log(obj.fn() === obj); // true
  26. console.log(obj2.method() === obj2); // true
  27. /*
  28. var obj2 = {
  29. method: obj.function(a,b) {
  30. return this;
  31. }
  32. };
  33. */
Add Comment
Please, Sign In to add comment