Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. // Example 1
  2. function identify() {
  3. return this.name.toUpperCase();
  4. }
  5. function speak() {
  6. var greet = "Hi, " + identify.call(this);
  7. console.log(greet);
  8. }
  9. var me = {name : "Kyle"};
  10. var you = {name : "Jen"};
  11.  
  12. identify.call(me); // KYLE
  13. identify.call(you); // JEN
  14. speak.call(me); // Hi, KYLE
  15. speak.call(you); // Hi, JEN
  16.  
  17.  
  18. // Example 2
  19. var add = function(x, y) {
  20. this.val = x + y;
  21. };
  22. var obj = {
  23. val: 0
  24. };
  25. add.apply(obj,[2,8]);
  26. // 1st, 2nd arguments
  27. console.log(obj, val); // 10
  28. add.call(obj,2,8);
  29. // 1st, 2nd, 3rd arguments
  30. console.log(obj, val); // 10
Add Comment
Please, Sign In to add comment