1

im a newbie javascripter, coming from c++/c#. im learning a tutorial which has the following lines:

​function getUserInput(firstName, lastName, callback, callbackObj)  {
​
    callback.apply (callbackObj, [firstName, lastName]);
}

this was an example of using a callback function inside another function.

and i was wondering about this line :

 callback.apply (callbackObj, [firstName, lastName]);

what is the meaning of using a [ ] inside a function?

melpomene
  • 79,257
  • 6
  • 70
  • 127
Yatir
  • 123
  • 2
  • 9

2 Answers2

1

Since you are using apply, the assumption is that the callback method signature has 2 string parameters: firstName and lastName. The callbackObj is the context being passed to the method (in case it will call this)

More info here

Tom Teman
  • 1,802
  • 2
  • 27
  • 43
  • @Yatir glad I could help. Please mark as accepted if that answered your question :) – Tom Teman Jan 29 '17 at 12:20
  • Actually, this is the assumption about `callback.apply`, i.e. it is assuming `callback.apply(foo, bar)` is equal to `Function.prototype.apply.call(callback, foo, bar)` – Paul S. Jan 29 '17 at 12:20
  • @PaulS. is right, I didn't notice the apply at first glance. This means the callback function actually expects two variables, not in an array - firstName and lastName. The first variable is the context being passed to the method. Edited my answer. – Tom Teman Jan 29 '17 at 12:24
1

This is a literal for an array. In this case it consists of 2 elements, the first being firstName the second lastName.