Posted By

alvaroisorna on 06/30/06


Tagged

javascript oop constructor


Versions (?)

namespaced constructor object


 / Published in: JavaScript
 

this snippet will help you understand how to use namespacing and oop construnction in JavaScript

  1. // namespaced constructor object
  2. var NAMESPACED_CONSTRUCTOR = function (){
  3. var _privateVar = 'NAMESPACED_CONSTRUCTOR';
  4. var _privateMethod = function (subparam){
  5. this.paramValue = subparam;
  6. alert(this.paramValue + ' instance of ' + _privateVar + ' created ');
  7. };
  8. _privateMethod.prototype.showValue = function (){
  9. alert(_privateVar + ', this object´s value is : ' + this.paramValue);
  10. };
  11.  
  12. // public instanciable items
  13. var _public = {
  14. Instance : _privateMethod
  15. };
  16.  
  17. alert(_privateVar + ' created');
  18. return _public;
  19. }();
  20.  
  21. window.onload = function (){
  22. var oNAMESPACED_CONSTRUCTOR = new NAMESPACED_CONSTRUCTOR.Instance('ok2');
  23. oNAMESPACED_CONSTRUCTOR.showValue();
  24. }

Report this snippet  

You need to login to post a comment.