/**
* 如果有自定义的YUI对象,将定义的对象复制到_YUI属性中
*/
if (typeof YUI != 'undefined') {
YUI._YUI = YUI;
}
/**
* 定义YUI全局对象
*
*/
var YUI = function() {
/**
* 定义实例对象属性 Y:在未实例化前this指向window,实例化后指向YUI对象
*/
var i = 0,
Y = this,
args = arguments,
l = args.length,
instanceOf = function(o, type) { // instanceof函数化
return (o && o.hasOwnProperty && (o instanceof type));
};
/**
* 自我实例化过程
* 当调用YUI()时,首次Y不是YUI实例,将new出一个对象
* 此时YUI()将再次从头到尾运行下来,此时Y已经是YUI对象了,本次将执行Y._init()方法
* 然后看闭包里面的实现
*/
if (!(instanceOf(Y, YUI))) {
Y = new YUI();
} else {
Y._init();
}
Y.instanceOf = instanceOf;
return Y;
};
(function() {
var proto, VERSION = '0.0.1';
proto = {
_init : function() {
var Y = this;
Y.constructor = YUI; //字面量声明,原型对象构造函数指回YUI
},
use:function(){
//
},
sayHello:function(name){
console.log('Hello YUI I am '+name);
}
};
YUI.prototype=proto; //YUI原型对象指向proto
})();
以上是YUI对象实例化的主要结构体,测试运行:
YUI().sayHello('steve');