Posted By

nerdfiles on 05/29/10


Tagged

javascript oop pattern design


Versions (?)

A design pattern?


 / Published in: JavaScript
 

  1. Book = (function(v1) {
  2. var _v1 = v1;
  3. return function(v2) {
  4. if (v2) {
  5. return v2;
  6. } else {
  7. return (_v1) ? {
  8. pub_v: _v1.replace(/_v1/, 'pub_v'),
  9. getTitle: function() {
  10. return "Got private through public getter: " + _v1;
  11. },
  12. getMe: function() {
  13. return this;
  14. }
  15. } : "Nuthin'!"
  16. }
  17. };
  18. })('Ethics1 (_v1 as v1)');
  19.  
  20. console.log( Book('Ethics2 (return v2)') + " & " + Book().getTitle() + " & static public: " + Book().pub_v + " & " + Book().getMe());
  21.  
  22. //o: Ethics2 (return v2) & Got private through public getter: Ethics1 (_v1 as v1) & static public: Ethics1 (pub_v as v1) & [object Object]
  23.  
  24. Book().getMe();
  25.  
  26. //o: [object Object] as { pub_v: .... }

Report this snippet  

You need to login to post a comment.