When writing a script I encountered such a problem: In the below attached fragment of the simplified code in which I will mark the desired section, when I call through apply() , an error appears on the second pass:

 Uncaught TypeError: CreateListFromArrayLike called on non-object 

That is, the first time it works correctly. And in the second pass an error occurs, even before entering the function. At the same time, if apply() replaced by call() , then everything starts working correctly. Perhaps I do not fully understand the difference in these methods. It seems the only difference is in the dynamic range of the apply() arguments. Although maybe I'm confusing something.

I would be glad if I was explained the reason for this behavior. Here is the whole code. Thank you in advance.

 function MetaRow(name,value){ this.name = name; this.value = value; } function MetaGroup(name){ this.name = name; this.items = {}; this.groups = {}; } MetaGroup.prototype.set = function(key,meta,replace = false) { if(meta instanceof MetaRow){ //Добавляем строку } else if(meta instanceof MetaGroup){ //Добавляем группу } } function FormMeta(forms) { this.forms = forms; this.Global = new MetaGroup(); FormMeta.prototype.update.apply(this); } FormMeta.prototype.get = function(key) { return this.Global.groups[key] != undefined ? this.Global.groups[key] : this.Global.groups['main']; } FormMeta.prototype.pickRadio = function(form) { var glob = this; $(form).find('radio-group').each(function(i,val){ var targetGroup,mid,rid,rname; //Далее я заполняю переменные ничего интересного //Вот на это строке в момент вызова apply() генерируется ошибка FormMeta.prototype.get.apply(glob,mid).set(rid, new MetaRow(rname),true); }) 

    1 answer 1

    http://www.w3schools.com/js/js_function_invocation.asp - apply accepts call parameters as an array:

     FormMeta.prototype.get.apply(glob, [mid]).set(rid, new MetaRow(rname), true); 

    or

     FormMeta.prototype.get.call(glob, mid).set(rid, new MetaRow(rname), true); 
    • Good. And, let's say, why does this work correctly on the first pass? - BwehaaFox
    • You do not notice something :). Also, the case may be in the logic of the code replaced by "// Next, I fill in the variables of nothing interesting . " What, for example, is equal to mid ? - Igor
    • It seems nothing special: var targetGroup = $(val).parent('meta-group').first(); var mid = $(targetGroup).attr('mid'); var targetGroup = $(val).parent('meta-group').first(); var mid = $(targetGroup).attr('mid'); - BwehaaFox
    • I see. That code which you resulted in how it is caused? - Igor
    • Perhaps due to the fact that he was called via call() : FormMeta.prototype.pickRadio.call(this,form); . But he will be called 1 time - BwehaaFox