Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // mind blown! custom property setters and getters in JavaScript!
- function Fancy() {
- var thing;
- Object.defineProperty(this, 'thing', {
- get: function(){
- return thing;
- },
- set: function(value){
- thing = value + '!';
- console.log("thing changed to '" + thing + '".');
- }
- });
- }
- var fancy = new Fancy();
- fancy.thing = 'herp';
- fancy.thing = 'derp';
- console.log('and the value was really saved as "' + fancy.thing + '".');
- // output:
- // thing changed to 'herp!".
- // thing changed to 'derp!".
- // and the value was really saved as "derp!".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement