Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. // The dummy class constructor
  2. function Class() {
  3. // I remove the initialization procedure in constructor function,
  4. // Initialization will done by Class.create which I defined below
  5. }
  6.  
  7. // Enforce the constructor to be what we expect
  8. Class.prototype.constructor = Class;
  9.  
  10. // And make this class extendable
  11. Class.extend = arguments.callee;
  12.  
  13. // What I improved
  14. Class.create = function () {
  15. var instance = new this();
  16. if (instance.init) {
  17. instance.init();
  18. }
  19. return instance;
  20. }
  21.  
  22. var Person = Class.extend({
  23. init: function(isDancing){
  24. this.dancing = isDancing;
  25. }
  26. });
  27.  
  28. var p = Person.create();
  29.  
  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.  
  39. // Add a new ._super() method that is the same method
  40. // but on the super-class
  41. this._super = _super[name];
  42.  
  43. // The method only need to be bound temporarily, so we
  44. // remove it when we're done executing
  45. var ret = fn.apply(this, arguments);
  46. this._super = tmp;
  47.  
  48. return ret;
  49. };
  50. })(name, prop[name]) :
  51. prop[name];
  52. }
  53.  
  54. Class.create = function () {
  55. var instance = new this();
  56. if (instance.init) {
  57. instance.init();
  58. }
  59. return instance;
  60. };
  61.  
  62. // The dummy class constructor
  63. function Class() {
  64. // All construction is actually done in the init method
  65. if ( !initializing && this.init )
  66. this.init.apply(this, arguments);
  67. }
  68.  
  69. Class.create = function () {
  70. var instance = new this;
  71. if (instance.init) instance.init.apply(instance, arguments);
  72. return instance;
  73. };
  74.  
  75. function CLASS(prototype) {
  76. var constructor = prototype.constructor;
  77. constructor.prototype = prototype;
  78. return constructor;
  79. }
  80.  
  81. var Person = CLASS({
  82. constructor: function (isDancing) {
  83. this.dancing = isDancing;
  84. },
  85. dance: function () {
  86. return this.dancing;
  87. }
  88. });
  89.  
  90. var p = new Person(true);
  91.  
  92. Function.prototype.extend = function (body) {
  93. var constructor = function () {};
  94. var prototype = constructor.prototype = new this;
  95. body.call(prototype, this.prototype);
  96. return constructor;
  97. };
  98.  
  99. Function.prototype.create = function () {
  100. var instance = new this;
  101. instance.constructor.apply(instance, arguments);
  102. return instance;
  103. };
  104.  
  105. var Person = Object.extend(function () {
  106. this.constructor = function (isDancing) {
  107. this.dancing = isDancing;
  108. };
  109.  
  110. this.dance = function () {
  111. return this.dancing;
  112. };
  113. });
  114.  
  115. var Ninja = Person.extend(function (base) {
  116. this.constructor = function () {
  117. base.constructor.call(this, false);
  118. };
  119.  
  120. this.swingSword = function () {
  121. return true;
  122. };
  123. });
  124.  
  125. var p = Person.create(true);
  126. p.dance(); // => true
  127.  
  128. var n = Ninja.create();
  129. n.dance(); // => false
  130. n.swingSword(); // => true
  131.  
  132. // Should all be true
  133. p instanceof Person && p instanceof Object &&
  134. n instanceof Ninja && n instanceof Person && n instanceof Object
  135.  
  136. function Factory(constructor, args) {
  137. return constructor.apply(this, args);
  138. }
  139.  
  140. Function.prototype.new = function () {
  141. Factory.prototype = this.prototype;
  142. return new Factory(constructor, args);
  143. };
  144.  
  145. jQuery === $ === function (a,b){return new e.fn.init(a,b,h)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement