Posted By

inkdeep on 06/17/09


Tagged

javascript js classes


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kcmr


Javascript Class with instance creation arguments


 / Published in: JavaScript
 

Namespace to avoid collision with other javascript variable/method names

  1. var NAMESPACE ={};
  2. NAMESPACE.MyClassName = function () { this.initialize.apply(this, arguments); };
  3. NAMESPACE.MyClassName.prototype = {
  4. value1: null,
  5. value2: null,
  6.  
  7. initialize: function(arg1, arg2) {
  8. this.value1 = arg1;
  9. this.value2 = arg2;
  10. },
  11.  
  12. addItUp: function () {
  13. var total = this.value1 + this.value2;
  14. return total;
  15. }
  16. };
  17.  
  18. // new instance
  19. var newInstance = new NAMESPACE.MyClassName('it ',' works!')
  20.  
  21. newInstance.addItUp();

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: kcmr on June 28, 2009

Packaging example: http://www.quizzpot.com/2009/03/paquetes-y-namespace/ (spanish)

You need to login to post a comment.