-
• #2
What if you declare b in line 22
var b = require('b').create();
, does it make a difference? -
• #3
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.
-
• #4
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 donew 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/Arrow_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. -
• #5
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.
-
• #6
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: { } >
-
• #7
@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...
Is there something missing in the es6 version to make it work?
board information:
code:
output: