Javascript Class with instance creation arguments


/ Published in: JavaScript
Save to your folder(s)

Namespace to avoid collision with other javascript variable/method names


Copy this code and paste it in your HTML
  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

You need to login to post a comment.