get and set on properties #336
Replies: 4 comments
-
Posted at 2014-12-23 by DrAzzy Gordon mentioned in the kickstarter comments (IIRC - it was somewhere) that getters and setters were one of the more exotic parts of javascript that are not implemented on Espruino. Here it is:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-12-23 by Sacha Many thanks DrAzzy It would be a nice feature in the espruino enviroment. With getters and setters nice objectoriented implementations could be made like: mycar.light='on'; // Hidden catch function implements the functionality If(mycar.ambientsensor<=5) { mycar.light='on'; } // Provided by the hidden getter function, done as above. Best regards Sacha |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-12-23 by @gfwilliams Hi Sacha, Thanks - I'm afraid it is quite far down my priority list at the moment though - as it doesn't actually allow you to do anything that you can't do already just by writing |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-12-23 by Sacha Hi Gordon No Problem. I just red a javascript book and discovered setters and getters the first time. Best regards Sacha |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-12-22 by Sacha
Hi Gordon
I just found learned the functionality of get and set.
http://jsconsole.com/?var%20person%20%3D%20%7B%0A%20%20_name%3A%20%27Marion%27%2C%0A%20%20get%20name()%20%7B%0A%20%20%20console.log(%27read%5Cn%27)%3B%0A%20%20%20return%20this._name%3B%0A%20%20%7D%2C%0A%20%20set%20name(value)%20%7B%0A%20%20%20%20console.log(%27write%5Cn%27)%3B%0A%20%20%20%20this._name%3Dvalue%3B%0A%20%20%7D%0A%7D%3B%0A%0Aperson.name%3D%27Michelle%27%3B
Code:
var person = {
_name: 'Marion',
get name() {
console.log('read\n');
return this._name;
},
set name(value) {
console.log('write\n');
this._name=value;
}
};
person.name='Michelle';
I just got a syntax error. Any chance that this will work on espruino too ?
Thanks
Sacha
Beta Was this translation helpful? Give feedback.
All reactions