Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The dummy class constructor
- function Class() {
- // I remove the initialization procedure in constructor function,
- // Initialization will done by Class.create which I defined below
- }
- // Enforce the constructor to be what we expect
- Class.prototype.constructor = Class;
- // And make this class extendable
- Class.extend = arguments.callee;
- // What I improved
- Class.create = function () {
- var instance = new this();
- if (instance.init) {
- instance.init();
- }
- return instance;
- }
- var Person = Class.extend({
- init: function(isDancing){
- this.dancing = isDancing;
- }
- });
- var p = Person.create();
- // Copy the properties over onto the new prototype
- for (var name in prop) {
- // Check if we're overwriting an existing function
- prototype[name] = typeof prop[name] == "function" &&
- typeof _super[name] == "function" && fnTest.test(prop[name]) ?
- (function(name, fn){
- return function() {
- var tmp = this._super;
- // Add a new ._super() method that is the same method
- // but on the super-class
- this._super = _super[name];
- // The method only need to be bound temporarily, so we
- // remove it when we're done executing
- var ret = fn.apply(this, arguments);
- this._super = tmp;
- return ret;
- };
- })(name, prop[name]) :
- prop[name];
- }
- Class.create = function () {
- var instance = new this();
- if (instance.init) {
- instance.init();
- }
- return instance;
- };
- // The dummy class constructor
- function Class() {
- // All construction is actually done in the init method
- if ( !initializing && this.init )
- this.init.apply(this, arguments);
- }
- Class.create = function () {
- var instance = new this;
- if (instance.init) instance.init.apply(instance, arguments);
- return instance;
- };
- function CLASS(prototype) {
- var constructor = prototype.constructor;
- constructor.prototype = prototype;
- return constructor;
- }
- var Person = CLASS({
- constructor: function (isDancing) {
- this.dancing = isDancing;
- },
- dance: function () {
- return this.dancing;
- }
- });
- var p = new Person(true);
- Function.prototype.extend = function (body) {
- var constructor = function () {};
- var prototype = constructor.prototype = new this;
- body.call(prototype, this.prototype);
- return constructor;
- };
- Function.prototype.create = function () {
- var instance = new this;
- instance.constructor.apply(instance, arguments);
- return instance;
- };
- var Person = Object.extend(function () {
- this.constructor = function (isDancing) {
- this.dancing = isDancing;
- };
- this.dance = function () {
- return this.dancing;
- };
- });
- var Ninja = Person.extend(function (base) {
- this.constructor = function () {
- base.constructor.call(this, false);
- };
- this.swingSword = function () {
- return true;
- };
- });
- var p = Person.create(true);
- p.dance(); // => true
- var n = Ninja.create();
- n.dance(); // => false
- n.swingSword(); // => true
- // Should all be true
- p instanceof Person && p instanceof Object &&
- n instanceof Ninja && n instanceof Person && n instanceof Object
- function Factory(constructor, args) {
- return constructor.apply(this, args);
- }
- Function.prototype.new = function () {
- Factory.prototype = this.prototype;
- return new Factory(constructor, args);
- };
- jQuery === $ === function (a,b){return new e.fn.init(a,b,h)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement