0

In the jstree plugin there is a function like this :

this.check_node = function (obj, e) {
    if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); }
    // Removed code for readability
};

So for the check_node I have to pass a list of arguments, the list of arguments I have to pass are in an array, so I do as following :

scope.jstree(true).check_node(...scope.args['check_node']);

Where scope.args['check_node'] contains the list of args I want to pass to check_node, doing this with spread operator won't raise any error, but for a reason I can't work with ES6 syntax and I'm stuck with ES5, so I had to use apply instead as following :

scope.jstree(true).check_node.apply(null, scope.args['check_node']);

But this will throw the following error :

TypeError: Cannot read property 'settings' of null

which is in this line :

if(this.settings.checkbox.tie_selection)

Why apply in this case is throwing the error and the spread oprator is not ? and how can I solve this ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • Just FWIW, `...` isn't an operator (and can't be, an operator has to have a single result value). It's just syntax. – T.J. Crowder Apr 04 '18 at 17:24

3 Answers3

0

Well, in your ES5 code you are explicitly passing null for thisArg, so this is null inside the called function. No surprise here.

The spread operator simply expands the array in a way such as if you would have passed its values on their own.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

Because you're explicitly saying "Make this during the function call null" by passing null as the first argument.

You need to pass the value you want to be this as the first argument.

I'm not familiar with your library, but this will do it:

var obj = scope.jstree(true);
obj.check_node.apply(obj, scope.args['check_node']);

If jstree returns scope, though, you can avoid the variable:

scope.jstree(true).check_node.apply(scope, scope.args['check_node']);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Because you are changing this of check_node with null.The first parameter that apply takes is the object/context which should act as this for the function that you have invoked.

khawar jamil
  • 166
  • 4