您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

80 行
2.0 KiB

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