JSFiddle - React, Tailwind, and code Playground

by captainstooby

JavaScript

//Write demethodize, a function that converts a method to a binary function
// EX:  demethodize(Number.prototype.add)(5, 6)
//EX result: 11

/*
var demethodizedValue = demethodize(add)(5,6);
console.log(demethodizedValue);


function demethodize(funcToDemethodize)
{
    return function (firstOperand)
    {
        return function (secondOperand)
        {
            return funcToDemethodize(firstOperand, secondOperand);
        }
    }
}

Number.Prototype.add = methodize(funcToDemethodize)
{
    methodize(funcToDemethodize);
}


function add(firstOperand, secondOperand)
{
    return firstOperand + secondOperand;
}

*/

/******************************************************************************************************************/

//Brian's solution to the following problem...
//Write demethodize, a function that converts a method to a binary function
// EX:  demethodize(Number.prototype.add)(5, 6)
//EX result: 11

Number.prototype.add = methodize(add);

function methodize(func) {//a function that converts a binary function to a method
    return function (x) {        
            //console.log(x);            
            //console.log(this);
            return func(x,this);            
        }
}

function demethodize(method) {//a function that converts a method to a binary function
        return function(x,y) {
            return method.call(x, y);
        };                  
}

function add(x, y) {
    return x + y;
}

console.log(demethodize(Number.prototype.add)(5,6)); //11