Actually it has the following code (arr - an array of properties) (KnockoutJS):

for (var prop in arr) { var val = this[arr[prop]]; if (val && ko.isSubscribable(val)) { (function (e, p) { e[p].subscribe(function (value) { e.processChange(p, value); }); })(this, arr[prop]); } } 

The question is: what happens to this when it is passed inside the circuit? Only link is copied? if so, will the subscribe method and processChange be called on this object of this code?

Reasons for doubt:

In the piece of code that is far from this, the object that was here this, one of its properties is called a knockout setter, which pulls the subscription.

BUT! the object that the setter is invoked is not equal to the e object when I stop in the subscription code for a breakpoint. And the subscription code is called just on the reaction of the setter.

In general, CHZH?

    1 answer 1

    Objects and arrays are passed by reference in any case (both as arguments and as closable variables)

     var obj1 = {}, obj2 = {}; var func = (function(obj ,prop, value){ return function(){ obj1[prop] = value; // как замыкаемая переманная obj[prop] = value; // как аргумент } })(obj2, 'prop', 'xxx'); obj1['prop'] = 'value'; obj2['prop'] = 'value'; func(); console.log(obj1); // {prop:'xxx'} console.log(obj2); // {prop:'xxx'} 

    therefore, in e will be (should be) a reference to the this object, that is, The subscribe method and processChange will processChange called on the this object of this code. Perhaps these are knockout nuances, or you lost context somewhere =)