Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. // mind blown! custom property setters and getters in JavaScript!
  2.  
  3. function Fancy() {
  4. var thing;
  5.  
  6. Object.defineProperty(this, 'thing', {
  7. get: function(){
  8. return thing;
  9. },
  10. set: function(value){
  11. thing = value + '!';
  12. console.log("thing changed to '" + thing + '".');
  13. }
  14. });
  15. }
  16.  
  17. var fancy = new Fancy();
  18. fancy.thing = 'herp';
  19. fancy.thing = 'derp';
  20. console.log('and the value was really saved as "' + fancy.thing + '".');
  21.  
  22. // output:
  23. // thing changed to 'herp!".
  24. // thing changed to 'derp!".
  25. // and the value was really saved as "derp!".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement