[转帖]Mootools源码分析-07 -- Document

原帖地址:http://space.flash8.net/space/?uid-18713-action-viewspace-itemid-400955

原作者:我佛山人

//Document对象,对document对象的扩展
var Document = new Native({

    
//族名,为$type提供精准类型判断
    name: 'Document',

    
//扩展原型
    legacy: (Browser.Engine.trident) ? null: window.Document,

    initialize: 
function(doc)    {
        
//生成唯一标识
        $uid(doc);

        
//建立两个快捷方式
        doc.head = doc.getElementsByTagName('head')[0];
        doc.html 
= doc.getElementsByTagName('html')[0];

        
//对当前文档所属window的快捷方式
        doc.window = doc.defaultView || doc.parentWindow;
        
if (Browser.Engine.trident4) $try(function()    {
            
//修正IE6以下不缓存背景图片的Bug
            doc.execCommand("BackgroundImageCache"falsetrue);
        });
        
return $extend(doc, Document.Prototype);
    },

    
//同时对当前window对象进行扩展
    afterImplement: function(property, value)    {
        document[property] 
= Document.Prototype[property] = value;
    }
});

Document.Prototype 
= {$family: {name: 'document'}};

new Document(document);


//对Document对象的扩展实现
Document.implement({

    
//创建新HTML Element
    newElement: function(tag, props)    {
        
//对于IE,这三个属性需要特别处理,同时IE支持直接使用标签代码创建HTML Element
        if (Browser.Engine.trident && props)    {
            [
'name''type''checked'].each(function(attribute)    {
                
if (!props[attribute])    return;
                tag 
+= ' ' + attribute + '="' + props[attribute] + '"';
                
if (attribute != 'checked'delete props[attribute];
            });
            tag 
= '<' + tag + '>';
        }
    
//返回moo化的对象
    return $.element(this.createElement(tag)).set(props);
    },

    
//创建文本节点
    newTextNode: function(text)    {
        
return this.createTextNode(text);
    },

    
//对document对象的引用
    getDocument: function()    {
        
return this;
    },

    
//对window对象的引用
    getWindow: function()    {
        
return this.defaultView || this.parentWindow;
    },

    
//打扫战场,防止内存泄漏
    purge: function()    {
        
var elements = this.getElementsByTagName('*');
        
for (var i = 0, l = elements.length; i < l; i++)    memfree(elements[i]);
    }

});

 

posted @ 2009-10-22 16:21  webgis松鼠  阅读(200)  评论(0)    收藏  举报