Uncaught TypeError: CreateListFromArrayLike called on non-object

The issue

When we tried to invoke a JavaScript method using apply method we got into the following issue:

Uncaught TypeError: CreateListFromArrayLike called on non-object

The reason

In JavaScript the apply method is available to function prototype. That means all the functions can be invoked with apply. But the apply method has a syntax. It expects all the parameters to be passed as array. If we don’t pass the parameters as array we get into the above issue (CreateListFromArrayLike called on non-object).

Lets consider the following example:

var userObj = {name: 'Jefferson smith'};
function getName(greeting){
    console.log(greeting + this.name);
}
getName.apply(userObj, 'Hello ');  
// Wrong: apply method expects arguments to be array but string given

The code will generate the following error:

Uncaught TypeError: CreateListFromArrayLike called on non-object

Solution

But if we pass the parameters as array like below then the error will be resolved:

var userObj = {name: 'Jefferson smith'};
function getName(greeting){
    console.log(greeting + this.name);
}
getName.apply(userObj, ['Hello ']); 
// Correct: apply method expects arguments to be array and array given