0

i am using javascript ES6 and babel. but i knew that apply doesn't work finally, i found solution

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);

at this link console.log.apply not working in IE9

so i replace from

const params = [
      level,
    ].concat(...args)

to

var params = [ level,]
var concat = Function.prototype.bind.call(params.concat, Array);
var paramsConcated =concat.apply(params, args)

but it printed out like this.

function Array() { ... },  [second arg...], [third arg..]

always print function Array() {...} for first argument.

do i have a misstake?

EDIT 1: i'm sorry that did not mention about output. i want to make paramsConcated to [level, args[0], args[1], ... , args[n]].

EDIT 2: after build, params.concat(...args) is replaced with concat.apply(params.args). so, i should not use 3 dots.

Community
  • 1
  • 1
Alta istar
  • 341
  • 1
  • 4
  • 11
  • Please [edit] your question to include a description of what the code is supposed to do. What is the desired output? – nnnnnn Jul 17 '16 at 02:02
  • So you feel that `.apply()` *"doesn't work"* in IE8, but you haven't defined what doesn't work and instead posted what you think is a solution. How about going back to the original problem. The `.apply()` *does* work, but it works according to ES3, which differs from ES5+. –  Jul 17 '16 at 02:18
  • `Function.prototype.bind.call(console.log, console)` is exactly the same as `console.log.bind(console)`. `Function.prototype.bind.call(params.concat, Array);`, if you needed it (which you don't), is exactly the same as 'params.concat.bind(Array)`. –  Jul 17 '16 at 03:34
  • @squint did you say that i can use apply if i use es3ify plugin? – Alta istar Jul 17 '16 at 05:21
  • @torazaburo you mean that use it like 'paramsConated=params.concat.bind(args)' ? – Alta istar Jul 17 '16 at 07:06

2 Answers2

1

The main problem is that you are binding concat() to the Array object instead of params. That's why you're getting function Array() in your output.

But what you're doing is unnecessary. console is a special case where its methods don't support .apply() in older versions of IE. Arrays do not have that problem.

So just do this:

var params = [ level,]
var paramsConcated = params.concat.apply(params, args)
JLRishe
  • 90,548
  • 14
  • 117
  • 150
0

You binded Array as the this value. That's what bind does.

I don't understand why you tried bind. It seems you only need a reference to [].concat.

var params = [ 123 ],
    args = [ [1], 2, [3] ],
    concat = Array.prototype.concat;
console.log(concat.apply(params, args)); // [ 123, 1, 2, 3 ]
console.log(params.concat(...args));     // [ 123, 1, 2, 3 ]
Oriol
  • 225,583
  • 46
  • 371
  • 457