`
kun10
  • 浏览: 12681 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
  • lz12366: 没有safia环境,没法测试、、上面代码divs.apply存 ...
    YArray.test

YUI读码笔记-解析YUI3的构造器原型(一)

    博客分类:
  • YUI
阅读更多
yui-base里面在定义了YUI构造器之后,构造器里面有一些实例方法,我们知道肯定是定义在了YUI的构造器原型上面的。
这里先解析_init方法,Y实例的一个初始化方法
_init: function() {
       var filter,
            Y = this,//Y已经是一个YUI的实例,this就是这个YUI实例,
            G_ENV = YUI.Env,//赋值YUI的Env,全局的YUI环境变量
            Env = Y.Env,//YUI实例的Env属性
            prop;

        /**
         * The version number of the YUI instance.
         * @property version
         * @type string
         */
        Y.version = VERSION;//获取版本号

        if (!Env) {//如果没有一些环境变量
            Y.Env = {
                mods: {}, // flat module map
                versions: {}, // version module map
                base: BASE,//这里BASE神马的都是定义在_init函数外面的。
                cdn: BASE + VERSION + '/build/',//拼接CDN地址,对于淘宝很多还面临环境切换的问题。
                // bootstrapped: false,
                _idx: 0,
                _used: {},
                _attached: {},
                _yidx: 0,
                _uidx: 0,
                _guidp: 'y',
                _loaded: {},
                serviced: {},
//getBase方法,看名字可以看得出来,是用来获取cdn的base的
//接受两个参数,srcPattern和comboPattern ,comboPattern是一个正则表达式
                getBase: G_ENV && G_ENV.getBase || function(srcPattern, comboPattern) {
        var b, nodes, i, src, match;
        // get from querystring
        nodes = (doc && doc.getElementsByTagName('script')) || [];//获取页面所有的script
        for (i = 0; i < nodes.length; i = i + 1) {
            src = nodes[i].src;
            if (src) {

                match = src.match(srcPattern);
                b = match && match[1];//为什么是match[1],这个和YUI的路径有关,就是说取到了这个文件
                if (b) {
                    // this is to set up the path to the loader.  The file
                    // filter for loader should match the yui include.
                    filter = match[2];

                    if (filter) {
                        match = filter.indexOf('js');

                        if (match > -1) {
                            filter = filter.substr(0, match);
                        }
                    }

                    // extract correct path for mixed combo urls
                    // http://yuilibrary.com/projects/yui3/ticket/2528423
                    match = src.match(comboPattern);
                    if (match && match[3]) {
                        b = match[1] + match[3];
                    }

                    break;
                }
            }
        }

        // use CDN default
        return b || Env.cdn;
    }
            };//结束YUI的Env的定义,getBase是获取base文件的方法。

            Env = Y.Env;//重新赋一遍值,这时候Env是一个有货的对象

            Env._loaded[VERSION] = {};//记录加载过的模块

            if (G_ENV && Y !== YUI) {//这里好奇怪,Y是YUI的实例,自然不等于YUI,这里有疑问
                Env._yidx = ++G_ENV._yidx;//G_ENV是YUI的静态属性,而Env._yidx是Y实例的index值。全局自增并赋值局部的index值。
                Env._guidp = ('yui_' + VERSION + '_' +
                             Env._yidx + '_' + time).replace(/\./g, '_');
            } else if (YUI._YUI) {//YUI._YUI是已经有YUI全局变量加载进页面,就从原来的YUI的上面取_yidx,

                G_ENV = YUI._YUI.Env;
                Env._yidx += G_ENV._yidx;//这里为什么不用自增的变量。保持原来的YUI的_yidx不变。
                Env._uidx += G_ENV._uidx;//uidx是另一个index值。

                for (prop in G_ENV) {
                    if (!(prop in Env)) {
                        Env[prop] = G_ENV[prop];//把G_ENV上面的参数记录到当前的Y实例上面。
                    }
                }

                delete YUI._YUI;//_YUI的任务光荣结束,想不通,
            }

            Y.id = Y.stamp(Y);
            instances[Y.id] = Y;//instances就是一个记录YUI实例的数组

        }

        Y.constructor = YUI;//记录Y实例的构造器

        // configuration defaults
        Y.config = Y.config || {//Y实例的配置
            win: win,
            doc: doc,
            debug: true,
            useBrowserConsole: true,
            throwFail: true,
            bootstrap: true,
            cacheUse: true,
            fetchCSS: true
        };

        Y.config.base = YUI.config.base ||
            Y.Env.getBase(/^(.*)yui\/yui([\.\-].*)js(\?.*)?$/,
                          /^(.*\?)(.*\&)(.*)yui\/yui[\.\-].*js(\?.*)?$/);//调用前面的getBase

        if (!filter || (!('-min.-debug.').indexOf(filter))) {
            filter = '-min.';
        }

        Y.config.loaderPath = YUI.config.loaderPath ||
            'loader/loader' + (filter || '-min.') + 'js';

    },
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics