---
title: "YUI3的YUI构造函数"
author: "kun10"
date: "2011-05-23T22:12:58Z"
tags: []
categories: ["Web前端"]
slug: "yui3-yui"
draft: false
lastmod: "2011-05-23T22:25:13Z"
id: 1055847
url: "https://f2eblog.iteye.com/blog/1055847"
---

YUI的构造函数


```java


var YUI = function() {
        var i = 0,
            Y = this,//用Y来保存YUI实例
            args = arguments,
            l = args.length,
            instanceOf = function(o, type) {//定义了instanceOf方法，用来看o是否为对象并且是否为某一个类型的实例
                return (o && o.hasOwnProperty && (o instanceof type));
            },
            gconf = (typeof YUI_config !== 'undefined') && YUI_config;//如果YUI_config有定义那么赋值给gconf

        if (!(instanceOf(Y, YUI))) {
//如果Y不是一个YUI的实例，就新new一个YUI实例
            Y = new YUI();
        } else {
//如果Y是YUI的实例就调用Y的_init方法
            Y._init();
            if (YUI.GlobalConfig) {//GlobalConfig是一个全局的配置，对所有的YUI实例都进行应用
                Y.applyConfig(YUI.GlobalConfig);
            }
//YUI_Config是一个页面级的配置。
            if (gconf) {//gconf保存的是页面级的配置，对同一个页面的所有YUI实例都起作用
                Y.applyConfig(gconf);
            }

            // bind the specified additional modules for this instance
//如果没有参数传给YUI函数，那么就直接执行_setup方法
            if (!l) {
                Y._setup();
            }
        }
//如果有参数，那么对参数逐个调用applyConfig方法，之后在调用_setup方法
        if (l) {
            // Each instance can accept one or more configuration objects.
            // These are applied after YUI.GlobalConfig and YUI_Config,
            // overriding values set in those config files if there is a '
            // matching property.
            for (; i < l; i++) {
                Y.applyConfig(args[i]);
            }

            Y._setup();
        }

        Y.instanceOf = instanceOf;//把instancOf方法绑到YUI实例上面

        return Y;
    };

```



这里面调用了几个Y实例的方法
_init
_setup
applyConfig
这几个方法是在YUI构造器的prototype
稍后做分析