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
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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.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:
Using the old
defineProperty
way:Or even inside the new classes: