We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

vcesjp on 01/20/08


Tagged

javascript textmate


Versions (?)


Who likes this?

1 person has 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.