I call concat()
on a string as shown below:
> "1".concat("2","3")
< "123"
Now I want to do this for the case where I have an array of strings to concat togther. But it doesn't do what I expect:
> "1".concat.apply(["2","3"])
< "2,3"
Not only is the first element missing, but a comma has been inserted between the two elements passed, like it was converting the argument in apply to a string and then returning that instead.
How can I use apply? I can't use String.prototype.concat.apply
because the first argument is actually a variable which could be string or array. I would rather not do some awful hack where I have to detect the type and then have a separate statement for each possible type the argument could be.
To be clear, I am trying to implement a function concat()
which works for any first argument type which makes sense (e.g. string or array). So far it looks like this, but is not working:
function concat(x) {
var args = Array.prototype.slice.call(arguments,1)
return x.concat.apply(args)
}