Avatar for AndreyVS

AndreyVS

Member since Feb 2021 • Last active Apr 2021
  • 2 conversations
  • 7 comments

Most recent activity

  • in General
    Avatar for AndreyVS

    Cool, many thanks! +1 to Espruino karma: recall Arduino, where one needs to reset pinMode to output after reading a pin..

  • in JavaScript
    Avatar for AndreyVS

    Many thanks, Robin! However, seems that this correctly references the instance, also supported by stackoverflow answer:

    Arrow functions do not have a this value of its own, when you access it, you are accessing the this value of the enclosing lexical scope.

    We can see if we print it out:

    function aClass(id) {
      this.id = id;
    }
    
    aClass.prototype.testEmit = function() {
      setTimeout(()=>{console.log('This is ', this); this.emit('TestEvent')}, 1000); 
    };
    
    var a = new aClass('a');
    a.on('TestEvent', ()=>console.log('a emitted')); 
    a.testEmit();  // get 'This is aClass {id = a...}' and the event is caught with 'a emitted' 
    
    var a1 = new aClass('a1');
    a1.on('TestEvent', ()=>console.log('a1 emitted')); 
    a1.testEmit(); // get 'This is aClass {id = a1...}' and the event is caught with 'a1 emitted' 
    

    By contrast, if we replace arrow function in L6 with function(), this will reference the global object, which does not have emit(), and code crashes.

    setTimeout(function(){console.log('This is ', this); this.emit('TestEvent')}, 1000);  //crashes
    

    To summarize for reference, this can be used, but should be cautious when calling setTimeout:

    function aClass() {}
    
    aClass.prototype.testEmit = function() {
      this.emit('TestEvent'); 
    };
    
    var a = new aClass();
    var a1 = new aClass();
    
    a.on('TestEvent', ()=>console.log('a emitted')); 
    a1.on('TestEvent', ()=>console.log('a1 emitted')); 
    
    setTimeout(a.testEmit.bind(a), 500); // can use, get printed 'a emitted'
    setTimeout(()=>a1.testEmit(), 1000); // can use, get printed 'a1 emitted'
    
    setTimeout(a.testEmit, 1500); //crashes, as *this* in testEmit definition references global object, not a
    
    
  • in JavaScript
    Avatar for AndreyVS

    Hm.. For whatever reason, it works now.. I'm a bit embarrassed.. Just for reference, code is here:

    function aClass() {};
    aClass.prototype.testEmit = function() {
      setTimeout(()=>this.emit('TestEvent'), 1000); 
    }
    var a = new aClass();
    a.on('TestEvent', ()=>console.log('Got event')); 
    a.testEmit();  // get 'Got event' printed in log
    var a1 = new aClass();
    a1.testEmit(); // event here is not caught
    
  • in JavaScript
    Avatar for AndreyVS

    Hi Everyone! On Puck I try to organize communication between objects with events (see Object.emit and discussion here). However, I cannot emit an event within a prototype function with 'this'. Thus if e.g. I have two step up modules, I cannot subscribe directly to each of them, and need to add either a module id as an event argument or additional empty object (see below).

    In comments here is what I'd want:

    function aClass() {};
    
    aClass.prototype.testEmit = function() {
      setTimeout(()=>aClass.emit('TestEvent'),­ 1000); //this.emit(...) here will not emit!
    }
    
    var a = new aClass();
    aClass.on('TestEvent', ()=>console.log('Got event')); //would like 'a.on(...)'
    a.testEmit();  // get 'Got event' printed in log
    
    var a1 = new aClass();
    a1.testEmit(); // get another 'Got event' printed in log
    

    Here is a way round with an empty object:

    function aClass() {
      this.emitter = {};
    };
    
    aClass.prototype.testEmit = function() {
      setTimeout(()=>this.emitter.emit('TestEv­ent'), 1000); //now it works
    }
    
    var a = new aClass();
    a.emitter.on('TestEvent', ()=>console.log('Got event')); //now we subscribe only to a
    a.testEmit(); //get 'Got event' in console
    
    var a1 = new aClass();
    a1.testEmit(); //get nothing
    

    Why can't I use this.emit(...)?

  • in General
    Avatar for AndreyVS

    Hi! Am I right that now one should not switch pin mode after analogRead(), and no initial pin mode setting is needed, to save power?

  • in General
    Avatar for AndreyVS

    Great, many thanks, it works! )

  • in General
    Avatar for AndreyVS

    Hi Everyone,
    Trying to debug using debugger. But if I run the function from the right-hand IDE side, it does nothing. While if testDebugger() is called then from the left-hand IDE side, it works. Can I make it working in both cases? Thanks!

    function testDebugger() {debugger;}
    
    testDebugger(); //does nothing
    
Actions