• Assume this simple Person 'class' definition with just value properties:

    var Person = function(name) {
      this.name = name;
    };
    

    Compare now both of these syntactically correct Person 'method' definitions:

    Definition 1:

    Person.prototype.spelled = 
        function() { return this.name.split("").join(" - "); };
    

    Definition 2:

    Person.prototype.spelled 
        = function() { return this.name.split("").join(" - "); };
    

    Why is 1 working and 2 throws the following runtime error:

    Uncaught SyntaxError: Got '=' expected EOF
     at line 1 col 5
        = function() { return this.name.split('').join(' = '); }...
    

    Has Espruino's rigid statement completion by semi-colon policy changed?

  • It's because when code is sent to Espruino it is executed as if you typed it in. If there is a newline at root scope and the statement by itself is complete then it is executed.

    It's done so it can execute more code than could actually be fitted into memory.

    Basically I've had to manually add hacks for the common ways people do strange formatting, but there are obviously a few ways I haven't covered.

    Just write code in the 'normal' coding style and it'll be fine. Would be good if you could file a bug on the Web IDE github for this though.

  • @Gordon, absolutely understood, because there is no compiler (if not explicitely said... and if there is or will be, it should behave the same way...)

    ...add hacks for the common ways people do strange formatting...

    me: guilty as charged.

    This case though was not a go for strange formatting, it was to fit neatly in the forum width... Even if something is done in the IDE to detect the issue, passing source code in the Modules.addCached(id,source) would still break... and there actually it happened
    first.

    Remembering the fact that there is no parsing to the end nor elaborate look ahead is good enough for me. Adding more look-ahead is performance counter-productive.

    May be over time some edited reference documentation will come into place to address the core differences between Espruino and (the) other JS execution environments.

  • That's actually a really strange one. Module.addCached shouldn't break with that at all as it parses everything as one string. Did you use the Web IDE for the module, or were you passing the code to addCached like in your other post?

    I've made a bug for it here: https://github.com/espruino/EspruinoWebIDE/issues/111

  • Module.addCached() does not break. It looks that I was mixing up different errors in my code when writing

    passing source code in the Modules.addCached(id,source) would still break... and there actually it happened
    first.

    (new lines inserted as code with escape are ignored...: initially, I stored the code as a module in the IDE's configured module folder. Then I took it from there - including the line-breaks - by adding them in the inline code concatenating strings - one for each line with ending new line... and meant to see the same error... but it was a nesting of quoting that lead to an error and I attributed it first to the line-break issue).

    I conclude that JS interpreter is just fine... (same behavior as return with arguments and statement closing semicolon on next line) but IDE is a bit out of sync with that for other language constructs...

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

Line break before/after '=' in ...prototype.method = function() {... works/fails

Posted by Avatar for allObjects @allObjects

Actions