Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. // Consider an object having a num property
  2. var obj = {num:2};
  3.  
  4. // Note the use of this in the function
  5. var addToThis = function(a,b,c){
  6. return this.num + a+b+c;
  7. };
  8.  
  9. // call is used to call a function on an object with the arguments passed along
  10. var result = addToThis.call(obj,4,3,4); // functioname.call(object_to_be_called, arguments_to_function)
  11. console.log(result); // 13
  12.  
  13. var array=[4,3,4];
  14. // apply is similar to call, except that it takes an array of arguments instead of comma separated values
  15. console.log(addToThis.apply(obj,array)); // 13
  16.  
  17. // bind returns a function whose this property points to the passed object, which can then be called using a comma separated list of arguments
  18. console.log(addToThis.bind(obj)(4,3,4));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement