/static/scripts/class.js

https://bitbucket.org/h_morita_dbcls/galaxy-central · JavaScript · 80 lines · 39 code · 21 blank · 20 comment · 5 complexity · 0a7e205be1ebe70f0430b507b141e028 MD5 · raw file

  1. /*!
  2. * Class definition
  3. *
  4. * Copyright (c) 2008 John Resig (http://ejohn.org/blog/simple-javascript-inheritance/)
  5. * Inspired by base2 and Prototype
  6. */
  7. "use strict";
  8. (function () {
  9. var initializing = false,
  10. fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  11. // The base Class implementation (does nothing)
  12. this.Class = function(){};
  13. // Create a new Class that inherits from this class
  14. Class.extend = function (prop)
  15. {
  16. var _super = this.prototype;
  17. // Instantiate a base class (but only create the instance, don't run the init constructor)
  18. initializing = true;
  19. var prototype = new this();
  20. initializing = false;
  21. // Copy the properties over onto the new prototype
  22. for (var name in prop)
  23. {
  24. // Check if we're overwriting an existing function
  25. prototype[name] = (typeof prop[name] === "function" && typeof _super[name] === "function" && fnTest.test(prop[name]) ?
  26. (function(name, fn)
  27. {
  28. return function()
  29. {
  30. var tmp = this._super;
  31. // Add a new ._super() method that is the same method
  32. // but on the super-class
  33. this._super = _super[name];
  34. // The method only need to be bound temporarily, so we
  35. // remove it when we're done executing
  36. var ret = fn.apply(this, arguments);
  37. this._super = tmp;
  38. return ret;
  39. };
  40. }(name, prop[name])) : prop[name]);
  41. }
  42. // The dummy class constructor
  43. function Class()
  44. {
  45. // All construction is actually done in the init method
  46. if (!initializing && this.init)
  47. {
  48. this.init.apply(this, arguments);
  49. }
  50. }
  51. // Populate our constructed prototype object
  52. Class.prototype = prototype;
  53. // Enforce the constructor to be what we expect
  54. Class.constructor = Class;
  55. // And make this class extendable
  56. Class.extend = arguments.callee;
  57. return Class;
  58. };
  59. }());