Simple getter and setter method depending on number of arguments


/ Published in: JavaScript
Save to your folder(s)

This example can be extended to include a more general object. here I get or set a single value


Copy this code and paste it in your HTML
  1. var value = false;
  2.  
  3. function gettersetter(bool){
  4. // _args = array with function arguments
  5. var _args = Array.prototype.slice.call(arguments);
  6. if(_args.length == 0){
  7. // if no arguments -> define get method
  8. return value;
  9. } else if(_args.length > 0){
  10. value = bool;
  11. }
  12. }
  13.  
  14. How to use it:
  15. // getter
  16. var newValue = gettersetter();
  17. // setter
  18. gettersetter(true);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.