Could someone explain to me the purpose of "set" and "get"?
To me they seem like methods that are not methods, kind of like a mix between variables and methods.Code:o = { a:7, get b() { return this.a+1; }, set c(x) { this.a = x/2; } }; alert(o.a); // 7 alert(o.b); // 8 o.c = 8; alert(o.a); // 4 alert(o.b); // 5
But the way I see people normally code (or with slight variations, such as prototypes and such), the object would be coded like:
Code:o = { a:7, b: function() { return this.a+1; }, c: function(x) { this.a = x/2; } }; alert(o.a); // 7 alert(o.b()); // 8 o.c(8); alert(o.a); // 4 alert(o.b()); // 5



Reply With Quote


Bookmarks