KISSY之KISSY.JS速记

阅读更多
https://github.com/kissyteam/kissy/blob/1.1.6/src/seed/kissy.js
(function(S, undef) {
    var meta = {
          /**
           * copy s到r,如果over, s[p]则overwrite r[p], 
           * 如果有whitelist,则只从s中copy whitelist中声明的p
           * @return r
           */
          mix: function(reciveObj, sourceObj, over, whitelist) {
                     ...}
    }
    host = this,

    seed = host[S] || {},

    guid = 0,
    EMPTY = '';

    if(!seed.mix) seed.mix = meta.mix;
    S = host[S] = seed; // shortcut

    S.mix(S, {

         // The host of runtime environment.
        __HOST: host,

        // S.app() with these members.
        __APP_MEMBERS: ['namespace'],
        __APP_INIT_METHODS: ['__init'],

        version: '@VERSION@',

        /**
         * 依次mix o1、o2至on,mix的时候直接overwrite
         */
        merge: function(/*o1,o2...on*/) {
            var o = {}, i, l = arguments.length;
            for (i = 0; i < l; ++i) {
                S.mix(o, arguments[i]);
            }
            return o;
        },

        /**
         * mix copy的是s[p]到r[p], augment是copy s.prototype[p]到r.prototype[p]
         * 如果s没有prototype的话,就copy s[p]
         */
        augment: function(/*receiveObj, srcObj1, srcObj2, ..., over, whitelist*/) {
                            ...},
        /**
         * r extend s, r.prototype overwrite mix px, r overwrite mix sx
         * r.superclass = s.protype
         */
        extend: function(r, s, px, sx) {
            if (!s || !r) return r;

            var OP = Object.prototype,
                O = function (o) {
                    function F() {
                    }

                    F.prototype = o;
                    return new F();
                },
                sp = s.prototype,
                rp = O(sp);

            r.prototype = rp;
            rp.constructor = r;
            r.superclass = sp;

            // assign constructor property
            if (s !== Object && sp.constructor === OP.constructor) {
                sp.constructor = s;
            }

            // add prototype overrides
            if (px) {
                S.mix(rp, px);
            }

            // add object overrides
            if (sx) {
                S.mix(r, sx);
            }

            return r;
        },
        __init: function() {
            this.Config = this.Config || {};
            this.Env = this.Env || {};
            this.Config.debug = '@DEBUG@';
        },
        namespace: function() { ...},
        /**
         * 为目标对象o(host[name]对象或name对象)实现APP功能,
         * 先从KISSY mix KISSY.__APP_MEMBERS,
         * 然后依次在o上执行KISSY.__APP_INIT_METHODS
         * 最后mix sx()的返回对象 或 sx对象
         * @param {String|Object}  name
         * @param {Function|Object} sx
         */
        app: function(name, sx) {
            var isStr = S.isString(name),
                O = isStr ? host[name] || {} : name,
                i = 0,
                len = S.__APP_INIT_METHODS.length;

            S.mix(O, this, true, S.__APP_MEMBERS);
            for(; i < len; ++i) S[S.__APP_INIT_METHODS[i]].call(O);

            S.mix(O, S.isFunction(sx) ? sx() : sx);
            isStr && (host[name] = O);

            return O;
        },
        log: function(msg, cat, src) { ...},
        error: function(msg) {...},
        guid: function(pre) { return (pre || EMPTY) + guid++;  }
     });
   S.__init();
})('KISSY');

你可能感兴趣的:(prototype,JavaScript,F#)