Home  >  Q&A  >  body text

javascript - A problem with call and apply in js?

Array.call(null,{length:3}//返回[{length:3}]
Array.apply(null,{length:3})//返回[undefined, undefined, undefined]

Both call and apply change the point of this, but the result here is different, which is weird

大家讲道理大家讲道理2639 days ago981

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-07-05 11:08:00

    Write it here for easy reading.

    call:
    The first one is to directly pass the object in;

    apply:
    The second one is treated as an array with an array length of 3, but there is no value, so undefined
    {length: 3} => {length: 3, 0: undefined, 1: undefined, 2: undefined }
    So Output 3 undefined

    call accepts continuous parameters, and apply accepts array parameters.
    A.call(this, a,b,c,d)
    A.apply(this, [a,b,c,d])

    reply
    0
  • 学习ing

    学习ing2017-07-05 11:08:00

    The second parameter of the call() method is an item in the array,

    The second parameter of the apply() method is an array

    reply
    0
  • Cancelreply