The problem with the function scope object

Posted on
  • There is an object created via new:

    function class_1()
    {
    this.A_1;
    this.B_1;
    }
    
    class_1.prototype.func_class_1 = function()
    {
        /*
            here are some actions with variables this.A_1, this.B_1
        */
    }
    
    function class_2 (_obj /*object of type class_1*/)
    {
    this.A_2;
    this.B_2;
    this.Obj_2 = _obj;
    }
    
    class_2.prototype.func_class_2  = function()
    {
        //function call problem !!!
        this.Obj_2.func_class_1();
    }
    
    var o1 = new class_1();
    var o2 = new class_2(o1);
    
    o2.func_class_2(); //Uncaught Error: Field or method "func_class_1()" does not already exist, and can't create it on undefined
    
    

    Help !
    I tried various options with this, call and bind, nothing helped. How to organize a call to "this.Obj_2.func_Class_1()" ?

  • Which firmware version are you running this on, and on what device?

    I just ran this on the latest builds and it works fine.

    However: If you put this in the right-hand side of the web IDE you get loads of errors. I'd fix those first.

    Specifically what do you intend to do with this.A_1;/etc? You're just referencing fields that don't exist yet, without setting them to anything.

  • Thank you for such a quick response !
    I use the cost of production of Amperka (http://amperka.ru/) - "IskraJS" based on Espruino 1v92.194

  • In the code there are no syntax errors it builds and runs fine, below part of code:

    function TObj_1(_pin)
    {   
        /*** константы класса ***/
        this.MinPulse = 453.0; //минимальная длительность импульса которая соответствует крайнему "меньшему" положения ротора
        this.MaxPulse = 2050.0; //макисмальная длительность импульса которая соответствует крайнему "меньшему" положения ротора
        this.Ks = 8.7; //коэффициент преобразования в ф-ции Y=kA+B (линейная кривая самого сервопривода
            
        /*** поля класса ***/
        this.Pin = null; //пин микропроцессора на котором "сидит" сервопривод
        this.Value = 0.0; //конечное значение яркости свечения
        this.CurValue = 0.0; //текущее значение яркости свечения
        this.Speed = 0.0; //текущая скорость процесса
    
        this.Pin = _pin; //задать пин к которому привязан сервопривод
    }
    TObj_1.prototype.BunRunAnimation = function()
    {
        this.FlagBunAnimation = true; //запретить "ручной" запуск Animation(...)
            this.StopAnimation(); //остановить текущую анимацию
    };
    
    function TObj_2(_tobj_1)
    {
        this.PHASE_ARR = [ "START_MOVE", "START_WAIT", "DOWN_MOVE", "DOWN_WAIT", "UP_MOVE", "UP_WAIT" ];
        
        this.ANGLE_START = 30; //конечный угол ротора фазы START_MOVE
        this.ANGLE_UP = 20; //конечный угол ротора фазы UP_MOVE
        this.ANGLE_DOWN = 0; //конечный угол ротора фазы DOWN_MOVE
        
        this.TIME_START = 15; //время нахождения в позиции START_WAIT, секунд
        this.TIME_UP = 0.5;  //время нахождения в позиции UP_MOVE, секунд
        this.TIME_DOWN = 0.7;  //время нахождения в позиции UP_WAIT, секунд
            
        this.SPEED_START = 25; //скорось движения ротора в фазе START_MOVE, %/s
        this.SPEED_UP = 40;  //скорось движения ротора в фазе UP_MOVE, %/s
        this.SPEED_DOWN = 25;  //скорось движения ротора в фазе DOWN_MOVE, %/s
        
        this.INTERVAL_PLAY = 60.0; //определяет период в ms запуска внутренней ф-и setTimeout
        
    
        this.Obj1 = _tobj_1; //инициируем поле объектом класса Servo
    };
    TObj_2.prototype.TaskRun = function()
    {
        let _this = this;
        
        if( !_this.FlagTaskRun )
            {
                _this.FlagTaskRun = true; 
                    _this.Obj1.BunRunAnimation(); //Error !
                
                _this.CurPhase = _this.PHASE_ARR[0]; 
            }
            
        switch(_this.CurPhase)
        {
            case "START_MOVE":
                                if( !_this.FlagSTART_MOVE )
                                {
                                    _this.Obj1.AnimationTask(_this.ANGLE_STA­RT, _this.SPEED_START);
                                        _this.FlagSTART_MOVE = true;
                                }
                                else
                                {
                                    if ( !_this.Obj1.GetStatusRunAnimation() ) //Error !
                                    {
                                        _this.CurPhase = _this.PHASE_ARR[1]; 
                                            _this.FlagSTART_MOVE = false; 
                                    }
                                }
                                
                break;
    };
    
  • Moving this to 'other boards' - IskraJS isn't an official board - they're just using the Open Source Espruino firmware.

    I've tried to run your code even on a 1v92 version of Espruino and it works (without the Exception) - perhaps you could contact Amperka to see if they can help you out.

  • Thanks again !
    Already sent a request to Amperka

  • The problem is solved. Loss of context occurred in one of the plugins. The firmware of the Amperka code executes correctly.

  • Great! By 'loss of context', you mean you called it in a setTimeout or something?

  • Yes, "this" was lost in one of setTimeout

  • JavaScript provides you with standard means to ensure the desired context:

    • You can pass parameter for the function in

    setTimeout(function(_, otherParms,...){
        _.memberOfContextObject;
        _.memberOfContextObject();
      }, timeoutTimeInMS, contextObject, otherParms,...);
    
    • Alternative is to bind the function and then pass to `

    setTimeout(function(otherParms,...){
        this.memberOfContextObject;
        this.memberOfContextObject();
      }.bind(contextObject), timeoutTimeInMS, otherParms,...);
    
    • This works too, but is not as flexible as above options:

    function anyFunctionOfThis() {
      var _this = this; // create non-this reference of this
      setTimeout(function(otherParms,...){
          _this.memberOfContextObject;
          _this.memberOfContextObject();
        }, timeoutTimeInMS, otherParms,...);
    

    As you know, you can defined the function as a named function outside of setTimeout(... or setInterval(... or ..., functionAsCallbackParm,...either as function or 'method of an object' (see object-oriented programming with JS). I use this when I have to provide them as callbacks in multiple places:

    var fun      = function(otherParms,...) { ... };
    var funBound = fun.bind(contextObject);
    
    setTimeout(funBound, timoutTimeInMS, otherParms,...
    

    There are many options and combinations there of you can chose from for the optimal use in your situation.

    For the alternative 'this', I use the _this or short form of it _ to indicate that it is about the this / context object. I've also seen that to be used. But I prefer a more eye catching - and short - form, therefore: _ is my choice.

  • Or.. if the contextObject you need is the outer scope just use a arrow function as setTimeout callback and be done with it.

    Other public choices for the this alter ego are me and self :D

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

The problem with the function scope object

Posted by Avatar for Konkery @Konkery

Actions