Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //why is it called partial?
- Function.prototype.partialApply = function(){
- var func = this;
- args = Array.prototype.slice.call(arguments); //why slice?
- return function(){
- return func.apply(this, args.concat(//what is apply doing in this case?
- Array.prototype.slice.call(arguments)//special arguments variable, what are we doing with it?
- ));
- };
- };
- //example of usage
- //create a function that converts numbers to hex values
- function nums2hex() {
- function componentToHex(component) {
- var hex = component.toString(16);
- // make sure the return value is 2 digits, i.e. 0c or 12
- if (hex.length == 1) {
- return "0" + hex;
- }
- else {
- return hex;
- }
- }
- return Array.prototype.map.call(arguments,
- componentToHex).join('');
- }
- var myOUI = 123;
- var example = nums2hex.partialApply(myOUI);//now can we chain because partialApply is a prototype of function object?
- console.log(example());// '7b'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement