Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. //why is it called partial?
  2. Function.prototype.partialApply = function(){
  3. var func = this;
  4. args = Array.prototype.slice.call(arguments); //why slice?
  5. return function(){
  6. return func.apply(this, args.concat(//what is apply doing in this case?
  7. Array.prototype.slice.call(arguments)//special arguments variable, what are we doing with it?
  8. ));
  9. };
  10. };
  11. //example of usage
  12. //create a function that converts numbers to hex values
  13. function nums2hex() {
  14. function componentToHex(component) {
  15. var hex = component.toString(16);
  16. // make sure the return value is 2 digits, i.e. 0c or 12
  17. if (hex.length == 1) {
  18. return "0" + hex;
  19. }
  20. else {
  21. return hex;
  22. }
  23. }
  24. return Array.prototype.map.call(arguments,
  25. componentToHex).join('');
  26. }
  27.  
  28. var myOUI = 123;
  29. var example = nums2hex.partialApply(myOUI);//now can we chain because partialApply is a prototype of function object?
  30. console.log(example());// '7b'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement