• Is there something missing in the es6 version to make it work?

    board information:

    >process.env
    ={
      VERSION: "2v08.117",
      GIT_COMMIT: "8af54959a",
      BOARD: "LINUX",
      FLASH: 262144, SPIFLASH: 262144, STORAGE: 262144, RAM: 0,
      SERIAL: "00000000-addeadde-addeadde",
      CONSOLE: "Telnet",
      MODULES: "Flash,Storage,hea" ... "crypto,tensorflow",
      EXPTR: 22978688 }
    

    code:

    // stable code version
    Modules.addCached("a",( function(){
      var _a,
          a = function(){_a = this;},
          pa = a.prototype;
      pa.sqrt = (a) => {return _a._sqrt(a);};
      pa._sqrt = (a) => {return Math.sqrt(a);};
      exports.create = function(){ return new a();};
    }));
    a = require('a').create();
    console.log(a.sqrt(3));
    
    // es6 code version
    Modules.addCached("b",(()=>{
      var _b,
          b = () => {_b = this;},
          pb = b.prototype;
      pb.sqrt = (a) => {return _b._sqrt(a);};
      pb._sqrt = (a) => {return Math.sqrt(a);};
      exports.create = () => { return new b();};
    }));
    b = require('b').create();
    console.log(b.sqrt(3));
    

    output:

    >1.73205080756
    Uncaught Error: Function "_sqrt" not found!
     at line 1 col 4
    _b._sqrt(a);
       ^
    in function "sqrt" called from line 26 col 21
    console.log(b.sqrt(3));
                        ^
    
  • What if you declare b in line 22 var b = require('b').create();, does it make a difference?

  • What if you declare b in line 22 var b = require('b').create();, does it make a difference?

    No, but a mix version of es6 and stable like this works:

    // es6 code version
    Modules.addCached("b",(()=>{
      var _b,
          //  b = () => {_b = this;},
          b = function() {_b = this;},   // <---
          pb = b.prototype;
      pb.sqrt = (a) => {return _b._sqrt(a);};
      pb._sqrt = (a) => {return Math.sqrt(a);};
      exports.create = () => { return new b();};
    }));
    b = require('b').create();
    console.log(b.sqrt(3));
    

    I guess it's a special constellation of arrow function usage that causes the interpreter to fail.

  • Thanks - that's interesting... Unlike normal functions, Arrow functions store the value of this when they are defined.

    In that code, it looks like they're expecting this to be something else though. Maybe ES6 defines some magic case where if you do new ArrowFunction, this is no longer what it's expected to be what it is in every other case.

    Actually MDN has this to say: https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Functions/Arro­w_functions#Use_of_the_new_operator

    Arrow functions cannot be used as constructors and will throw an error when used with new.

    So it looks like the code is invalid. And if you try and run even a small snippert in Node.js, it fails:

    var _b, b = () => {_b = this;};
    new b()
    // Uncaught TypeError: b is not a constructor
    

    Where did the code come from? If anything Espruino's failing here appears to be not producing an error when new ArrowFn is used.

  • I guess it's a special constellation of arrow function usage that causes the interpreter to fail.

    Not at all!

    Reason: There is no this in arrow functions - that‘s all.

  • Where did the code come from?

    From trying to shrink js code.

    Your code snippet does not produce an error, that made me stumble over this and thinking there is a special implementation of arrow functions.

    copy & paste WebIDE left side:

    >var _b, b = () => {_b = this;};
    =function (undefined) { ... }
    >new b()
    =b: {  }
    > 
    
  • @MaBe/@Gordon, did not notice that a() was defined as 'classical'/regular function but b() was defined as arrow function. That was by the way the reason that I did not change the TMQ regular - constructor - function in tinyMQTT to an arrow function, but everything else I did convert. I was first even concerned to change the prototype functions into arrow functions... but since they do not use this there is no problem...

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

stable and es6 code version produce different output

Posted by Avatar for MaBe @MaBe

Actions