You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class.js 2.1 KiB

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