Posted By

inamorix on 01/09/08


Tagged

js


Versions (?)

Class (instanceof Class)


 / Published in: JavaScript
 

  1. function Class (ns) {
  2. this.toString = function () { return ns; };
  3. }
  4. Class.def = function (ns, ctx) {
  5. var c = window;
  6. var ns = ns.split('.');
  7. for (var i = 0, ix = ns.length - 1; i < ix; i++)
  8. c = c[ns[i]] instanceof Object ? c[ns[i]] : c[ns[i]] = {};
  9.  
  10. if (!(ctx.init instanceof Function)) ctx.init = function () {};
  11. c = c[ns[i]] = function () { this.constructor.apply(this, arguments); };
  12. c.prototype = new Class(ns.join('.'));
  13. c.prototype.constructor = ctx.init;
  14. for (var key in ctx) if (ctx.hasOwnProperty(key)) {
  15. if (key != 'init') c.prototype[key] = ctx[key];
  16. }
  17.  
  18. return c;
  19. };

Report this snippet  

You need to login to post a comment.