• I've just added the following to cutting-edge Espruino builds. These will be shipped in Espruino releases 1v100 and later (1v99 doesn't have them):

    for (..of..)

    This is a nice way of iterating over array/object properties, and will support iterators in the future. It's a more sane version of for (..in..) that returns values not keys, and that doesn't iterate over inherited properties.

    >var arr = ["One", "Two", "Three"];
    =[ 
      "One", 
      "Two", 
      "Three"
     ]
    >for (var i of arr) console.log(i)
    One
    Two
    Three
    

    Getters and setters

    You can now call a function when a value is read or written in an object. These work in normal object definitions:

    >var foo = {
    :  set contents(x) { this._contents = x; },
    :  get sum() { 
    :    var s=0;
    :    for (var i of this._contents) s+=i;
    :    return s;
    :  }
    :};
    ={  }
    >foo.contents = [1,2,3,4];
    =undefined
    >foo.sum
    =10
    

    Using the old defineProperty way:

    >var o = {a: 41};
    ={ "a": 41 }
    >Object.defineProperty(o, 'b', { get: function() { return this.a + 1; } });
    ={ "a": 41 }
    >o.b
    =42
    

    Or even inside the new classes:

    >class Example {
    :  get hello() {
    :    return 'world';
    :  }
    :}
    =function () {}
    >const obj = new Example();
    =Example: {  }
    >obj.hello
    ="world"
    
  • nice!

  • Nice! That was quick :)

  • It's good news! Thanks!

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

for (..of..), getters and setters now added to Espruino

Posted by Avatar for Gordon @Gordon

Actions