简体   繁体   中英

Jquery plugin, FlipClock.js code understanding

can someone explain to me what is "Base.extend" function do in the code

Base.extend = function(_instance, _static) { // subclass
    
    "use strict";
    
    var extend = Base.prototype.extend;
    
    // build the prototype
    Base._prototyping = true;
    
    var proto = new this();
    
    extend.call(proto, _instance);
    
    proto.base = function() {
    // call this method from any other method to invoke that method's ancestor
    };

    delete Base._prototyping;
    
    // create the wrapper for the constructor function
    //var constructor = proto.constructor.valueOf(); //-dean
    var constructor = proto.constructor;
    var klass = proto.constructor = function() {
        if (!Base._prototyping) {
            if (this._constructing || this.constructor == klass) { // instantiation
                this._constructing = true;
                constructor.apply(this, arguments);
                delete this._constructing;
            } else if (arguments[0] !== null) { // casting
                return (arguments[0].extend || extend).call(arguments[0], proto);
            }
        }
    };
    
    // build the class interface
    klass.ancestor = this;
    klass.extend = this.extend;
    klass.forEach = this.forEach;
    klass.implement = this.implement;
    klass.prototype = proto;
    klass.toString = this.toString;
    klass.valueOf = function(type) {
        //return (type == "object") ? klass : constructor; //-dean
        return (type == "object") ? klass : constructor.valueOf();
    };
    extend.call(klass, _static);
    // class initialisation
    if (typeof klass.init == "function") klass.init();
    return klass;
};

I have got this code from github repo, I can't understand what is the use of Base.extend function

https://github.com/objectivehtml/FlipClock/blob/master/compiled/flipclock.js

In old(er) scripts you will find code like this a lot. It works (or is supposed to work) similar to the behavior of class Child extends Parent {} and is meant to provide inheritance of properties/functions.

Before the introduce of the class keyword, it was a bit harder to assign multiple constructors to an object.

Check out Object.setPrototypeOf to find out more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM