/ Published in: JavaScript
O'Reilly Media | JavaScript: The Definitive Guide http://www.oreilly.com/catalog/jscript5/
Expand |
Embed | Plain Text
/* * O'Reilly Media | JavaScript: The Definitive Guide * http://www.oreilly.com/catalog/jscript5/ */ function makeProperty (o,name,predicate) { var value; o["get"+name]=function() { return value; }; o["set"+name]=function(v) { if( predicate && !predicate(v) ){ throw new Error("set"+name+": invalid value "+ v); }else{ value=v; } }; } var o={}; makeProperty(o,"Name", function(x) { return typeof x=="string"; } ); o.setName("myObj"); print(o.getName());
You need to login to post a comment.
