Adding Multiple Arguments into an built in function, from a function

164 views Asked by At

I'm trying to add String.fromCharCode(). And adding it into a function that renames it like so:

function from() {
    var ar = arguments.length;
    var argumentss = '';
    for (var i = 0; i < ar; i++) {
        var a = argumentss + '\'' + arguments[i];
           if (i == ar - 1) {
               argumentss = a + '\''
           } else {
               argumentss = a + '\', '
           }
    }
    var arg = eval(argumentss);
    return String.fromCharCode(arg)
}

I need to do this, so don't say that there is no reason to do this, since the way im actually using it there is a reason.

What im trying to do is make it possible to do:

from(65,66,67) //returns ABC

Without doing function from(a,b,c)

Since with the fromCharCode you can do as many arguments as you want. It would also be useful if there was a custom fromCharCode function. Thanks

2

There are 2 answers

1
Engineer On BEST ANSWER

Is this what you want?

function from() {
    var result = '';
    for (var i = 0; i < arguments.length; i++) {
       result += String.fromCharCode(arguments[i]);
    }
    return result;
}

from(65,66,67)
//result ->  ABC
0
Tomasz Nurkiewicz On

Does this address your problem? Call this once at the beginning:

String.from = String.fromCharCode.bind(String)

and use later:

String.from(65, 66, 67)  //"ABC"

Using built-in String.fromCharCode().