Posted By

alvaroisorna on 06/30/06


Tagged

javascript oop namespaces


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

expertseries
kele5240


namespaced function


 / Published in: JavaScript
 

this example will help you understand how to use namespacing in JavaScript

  1. // namespaced function
  2. var NAMESPACED = function (param){
  3. var _privateVar = '_privateMethod loaded: ';
  4. var _privateMethod = function (subparam){
  5. var cSubparam = (subparam) ? subparam : '';
  6.  
  7. alert(_privateVar + param + cSubparam);
  8. }(' during declaration');
  9.  
  10. var _public = {
  11. publicVar : _privateVar,
  12. publicVar2 : 'public Method',
  13. publicMethod : function(subparam){
  14. var cSubparam = (subparam) ? subparam : '';
  15.  
  16. alert(this.publicVar2 + ': ' + param + cSubparam);
  17. },
  18. publicMethod2 : function(){
  19. // ...
  20. }
  21. };
  22.  
  23. return _public;
  24. }('ok');
  25.  
  26. window.onload = function (){
  27. NAMESPACED.publicMethod(' on load');
  28. }

Report this snippet  

You need to login to post a comment.