Posted By

vcesjp on 01/20/08


Tagged

javascript textmate


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

arcturus


makeProperty


 / Published in: JavaScript
 

O'Reilly Media | JavaScript: The Definitive Guide http://www.oreilly.com/catalog/jscript5/

  1. /*
  2. * O'Reilly Media | JavaScript: The Definitive Guide
  3. * http://www.oreilly.com/catalog/jscript5/
  4. */
  5. function makeProperty (o,name,predicate) {
  6. var value;
  7.  
  8. o["get"+name]=function() { return value; };
  9.  
  10. o["set"+name]=function(v) {
  11. if( predicate && !predicate(v) ){
  12. throw new Error("set"+name+": invalid value "+ v);
  13. }else{
  14. value=v;
  15. }
  16. };
  17. }
  18.  
  19. var o={};
  20. makeProperty(o,"Name", function(x) { return typeof x=="string"; } );
  21. o.setName("myObj");
  22. print(o.getName());

Report this snippet  

You need to login to post a comment.