0

I've been reading Nicholas Zakas 'The Principles of Object Oriented Javascript' and came across an example using apply(). I messed with the example and ended up having the apply method read the array as a string. I cannot explain this functionality:

function testFunction(theArray) {
      console.log(Array.isArray(theArray)); // false
      console.log(theArray); // string1
      console.log(theArray[0]); // s
      console.log(this.name); // testName
    }
    var person1 = {name:'testName'};

    testFunction.apply(person1,['string1','string2']);

Could somebody please explain how I am getting this result and why I can't seem to access the array?

Thanks

Gerard Simpson
  • 1,757
  • 1
  • 23
  • 40
  • 1
    read what [function apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) does and comapre with [function call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) – Jaromanda X Aug 06 '16 at 00:39
  • Because `apply` works like this. It seems you confused it with `call`. – Oriol Aug 06 '16 at 00:39
  • This is an excerpt from that link: 'apply is very similar to call(), except for the type of arguments it supports. You can use an arguments array instead of a named set of parameters. With apply, you can use an array literal, for example, fun.apply(this, ['eat', 'bananas']), or an Array object, for example, fun.apply(this, new Array('eat', 'bananas'))' – Gerard Simpson Aug 06 '16 at 00:41

0 Answers0