JavaScript日誌操作對象執行個體

來源:互聯網
上載者:User

        學完JavaScript,部分小結可參看我以前的部落格《JavaScript中的Regex》、《JavaScript中的細節》、《自建JavaScript函數庫》、《JavaScript中的物件導向》、《JavaScript中的範圍和閉包》、《JavaScript中的繼承和原型》。現在我們來綜合使用一下所學知識,來實現一個JavaScript日誌操作對象的執行個體,我們需要的是這幾個檔案:

        myLog.js:主要作用是構建myLogger建構函式、添加行、添加節點、css控制。

        LD.js:主要作用是對指令碼和字串制定規則、構造命名空間和根據Id、className取出對象。

        test.js:主要作用是對表單添加事件,並測試mylog中部分函數的可用性。

        log.html:用於顯示日誌對象。

以下是各個檔案代碼:

test.js

//向window對象裡面添加一個load事件LD.addEvent(window,'load',function(){LD.log.writeRaw('This is raw');LD.log.writeRaw('<strong>This is bold!</strong>');LD.log.header('With a header');LD.log.write('write source:<strong>This is bold!</strong>');for(i in document){        LD.log.write(i);}});

myLog.js

// JavaScript Document//myLogger的建構函式function myLogger(id){id=id||"ICLogWindow";//日誌表單的引用var logWindow=null;//建立日誌表單var createWindow=function(){        var browserWindowSize = LD.getBrowserWindowSize();var top=(browserWindowSize.height-200)/2||0;var left=(browserWindowSize.width-200)/2||0;//使用ULlogWindow=document.createElement("UL");//在document下添加一個dom對象UL//添加ID進行標識      logWindow.setAttribute("id",id);//對表單進行css樣式控制logWindow.style.position='absolute';logWindow.style.top=top+'px';logWindow.style.left=left+'px';logWindow.style.width='200px';logWindow.style.height='200px';logWindow.style.overflow='scroll';logWindow.style.padding='0';logWindow.style.margin='0';logWindow.style.border='1px solid black';logWindow.style.backgroundColor='white';logWindow.style.listStyle='none';logWindow.style.font='10px/10px Verdana, Tahoma, Sans';//將表單添加到頁面上面document.body.appendChild(logWindow);}//向日誌表單中添加一行this.writeRaw=function(message){        //如果初始表單是不存在的,則組建記錄檔表單if(!logWindow){createWindow();}//建立li的dom節點var li=document.createElement('LI');//對表單進行css樣式控制li.style.padding='2px';li.style.border='0';li.style.borderBottom='1px dotted black';li.style.margin='0';li.style.color='#000';//驗證message資訊if(typeof message == 'undefined'){        //在li裡面添加文本節點。li.appendChild( document.createTextNode('Message is undefined')   );}else if(typeof li.innerHTML!=undefined){        //這是另一種方式的表達li.innerHTML=message;}else{li.appendChild(document.createTextNode(message)   );}logWindow.appendChild(li);return true;};}//對象字面量的方式聲明特權方法//向日誌表單中添加一行,向輸入的內容進行簡單處理myLogger.prototype={write:function(message){if(typeof message=='string' && message.length==0 ){        return this.writeRaw('沒有輸入資訊');}if(typeof message !='string'){if(message.toString){return this.writeRaw(message.toString());}else{return this.writeRaw(typeof message);}}//將大於符號小於符號進行正則轉換成HTML標記message=message.replace(/</g,"<").replace(/>/g,">");return this.writeRaw(message);},header:function(message){message='<span style="color:white;background-color:black;font-weight:bold;padding:0px 5px;">'+message+'</span>';return this.writeRaw(message);}};window['LD']['log'] = new myLogger();

LD.js

// JavaScript Documentif(document.all && !document.getElementById){document.getElementById=function(id){return document.all[id];}}if(!String.repeat){String.prototype.repeat=function(l){return new Array(l+1).join(this);}}if(!String.trim){String.prototype.trim=function(){return this.replace(/^\s+|\+$/g,'');} }(function(){//構造命名空間window['LD']={};  function $(){var elements=new Array();//arguments當前函數的參數數組。參數for(var i=0;i<arguments.length;i++){var element=arguments[i];if(typeof element=='string'){element=document.getElementById(element);}if(arguments.length==1){return element;}elements.push(element);}return elements;}//註冊命名空間window['LD']['$']=$;function getElementsByClassName(className,tag){parent=parent || document;if(!(parent=$(parent))) return false;//var allTags=document.getElementsByTagName(tag);//對tag進行過濾,把tag的對象全取出來var allTags=(tag == "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag);var matchingElements=new Array();className=className.replace(/\-/g,"\\-");var regex=new  RegExp("(^|\\s)"+className+ "(\\s|$)");var element;for(var i=0;i<allTags.length;i++){element=allTags[i];if(regex.test(element.className)){matchingElements.push(element);}}//返回這個數組return matchingElements;}window['LD']['getElementsByClassName']=getElementsByClassName;function bindFunction(ojb,func){return function(){func.apply(obj,arguments);}};window['LD']['bindFunction']=bindFunction;function getBrowserWindowSize(){var de=document.documentElement;return{'width':(window.innerWidth|| (de && de.clientWidth)|| document.body.clientWidth),'heigth':(window.innerHeight|| (de && de.clientHeight)|| de && document.body.clientHeight)}};//註冊本事件window['LD']['getBrowserWindowSize']=getBrowserWindowSize;function addEvent(node,type,listener){if(!(node=$(node))) return false;if(node.addEventListener){   node.addEventListener(type,listener,false);return true;   }else if(node.attachEvent){   node['e'+type+listener]=listener;node[type+listener]=function(){node['e'+type+listener](window.event);}node.attachEvent('on'+type, node[type+listener]);return true;   }   return false;};//註冊本事件window['LD']['addEvent']=addEvent;})();

運行結果:

總結

        這個小例子,基本上把以前所學內容,包括基礎支援、物件導向、原型、對象字面量、this、範圍鏈等知識點全部囊括,算是對JavaScript學習的一個小結。學的越多,越要找到所學內容之間的聯絡,學習JS,不只是僅僅學習JS,更要聯絡以前所學的物件導向、C#、CSS等知識,讓知識像路一樣四通八達,才能“書越讀越薄”。現在能做的,就是繼續構建我的知識網。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.