Jump to content

"this" referencing a different object instance


jimfog

Recommended Posts

Take a look at these 3 classes:

 var EventDragging = /** @class */ (function (_super) {
        __extends(EventDragging, _super);//it seems this code sets the obj prototype
        function EventDragging(settings) {
            var _this = _super.call(this, settings) || this;
            //some props here
            _this.handlePointerDown = function (ev) {
              
            };
            var hitDragging = _this.hitDragging = new HitDragging(_this.dragging, interactionSettingsStore);
            hitDragging.emitter.on('pointerdown', _this.handlePointerDown);

            return _this;
        }
       
        return EventDragging;
    }(Interaction));

 var Emitter = /** @class */ (function () {
        function Emitter() {
            this.handlers = {};
            this.thisContext = null;
             }
         Emitter.prototype.on = function (type, handler) {
            addToHash(this.handlers, type, handler);
        };
        Emitter.prototype.trigger = function (type) {//η select callback μαλλον πυροδοτειται απο εδω
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
         
            var attachedHandlers = this.handlers[type] || [];
            var optionHandler = this.options && this.options[type];
            var handlers = [].concat(optionHandler || [], attachedHandlers);
            for (var _a = 0, handlers_1 = handlers; _a < handlers_1.length; _a++) {
                var handler = handlers_1[_a];
                handler.apply(this.thisContext, args);//handler called here
            }
        };
     
        return Emitter;
    }());

 var HitDragging = /** @class */ (function () {
        function HitDragging(dragging, droppableStore) {
            var _this = this;
            this.handlePointerDown = function (ev) {
            _this.emitter.trigger('pointerdown', ev);//goes to  pointerdown method of EvenDragging Class
            };
           }
       };
        return HitDragging;
    }());

Inside the EventDragging class a new Instance of HitDragging class is created....and a listener attached(on)...this patter is event/emitter patter from Node.js and since this is not node a polyfill is made-look at the on method inside the emitter class.

The code for triggering the handlePointerDown method inside the EventDragging class is found inside the HitDragging class..in the trigger method call(also taken from the emitter class)

The codebase I study has various instances of The HitDragging class-in the above example you see one of them

Each time a different instance is accesed-the value of this references(depending on some factors) a different instance of the Htdragging class.

Despite searching extensively I have not found the code responsible for this reference change.-furthermore there is nowhere a bind method or arrow function to set the reference accordingly.

Keep in must that this code is an emulation of event emiiter patter(pub/sub) found in node.

Node has native functions for the above(on/trigger method).

Any idea as to where I shoud direct my search...a clue maybe.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...