【写一个自己的js库】 2.实现自己的调试日志
还是本着学习的目的,实现一个自己的调试日志,界面很简单,就是将调试信息显示在页面的正中央,用一个ul包裹,每条信息就是一个li。
1.新建一个myLogger.js文件,将需要的方法声明一下。其中var声明的是私有成员,可见范围只在构造函数中,每个实例都会保存一套他们的副本。this声明的是特权方法,new的时候会把它绑定到实例上,实例可以直接调用它。在prototype上声明的就是公有方法了,每个实例都可以访问它。最后将一个实例赋值给Lily这个库,Lily就有自己的日志插件了。
function myLogger(id){
id = id || 'LilyLogWindow';
var logWindow = null;
var createWindow = function (){};
this.writeRaw = function (){};
}
myLogger.prototype = {
write : function (message){},
header: function (message){}
};
if(!window.Lily) { window.Lily = {}; }
window['Lily']['log'] = new myLogger();
2.先给Lily补充一个方法,因为要显示在页面的中央,所以要获得浏览器窗口的高度和宽度,所以加个getBrowserWindowSize。window.innerWidth和window.innerHeight IE8及之前的版本不支持,document.documentElement在IE6的怪异模式下获取的值不正确。
function getBrowserWindowSize(){
var de = document.documentElement;
return {
width : window.innerWidth || (de && de.clientWidth) || (document.body.clientWidth),
height: window.innerHeight || (de && de.clientHeight) || (document.body.clientHeight)
};
}
Lily['getBrowserWindowSize'] = getBrowserWindowSize;
3.createWindow。这里就设置ul是固定的宽高,这里没用外链css,是因为如果想调试还要加个css,比较麻烦,所以就都在js里写好了。
var createWindow = function (){
var windowSize = Lily.getBrowserWindowSize();
var top = (windowSize.width - 200)/2 || 0;
var left = (windowSize.height - 200)/2 || 0;
logWindow = document.createElement("ul");
logWindow.setAttribute("id", id);
logWindow.style.position = "absolute";
logWindow.style.left = left + "px";
logWindow.style.top = top + "px";
logWindow.style.width = "200px";
logWindow.style.height = "200px";
logWindow.style.overflow = "scroll";
logWindow.style.border = "1px solid #f1f1f1";
logWindow.style.padding = "0";
logWindow.style.margin = "0";
logWindow.style.listStyle = "none";
document.body.appendChild(logWindow);
};
4.writeRaw,当调用这个方法时,才检查logWindow是否创建,如果没创建,就调用createWindow,注意不要加this,因为createWindow是私有变量,myLogger的实例没有这个方法,而根据闭包,writeRaw可以访问到createWindow。虽然各浏览器都支持innerHTML,但是它不属于w3c规范,所以在用之前要进行一下能力检测,以防以后的浏览器不支持。
this.writeRaw = function (message){
if(!logWindow) createWindow();
var li = document.createElement("li");
li.style.padding = "2px";
li.style.margin = "0";
li.style.borderBottom = "1px dotted #f0f0f0";
if(typeof message == "undefined"){
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;
};
5.write和header。这里write要处理一下信息,去掉html格式,不是string类型的调用toString或者显示它的类型。header就是将message包裹一下。
myLogger.prototype = {
write : function (message){
if(typeof message != 'string'){
if(message.toString){
return this.writeRaw(message.toString());
}else{
return this.writeRaw(typeof message);
}
}
if(typeof message == 'string' && message.length == 0){
return this.writeRaw("Lily log : message is null");
}
message = message.replace(/</g, '<').replace(/>/g, ">");
return this.writeRaw(message);
},
header: function (message){
message = '<span style="color:#fff;background-color:#000;font-weight:bold;padding:0 5px">' + message + '</span>';
return this.writeRaw(message);
}
};
6.测试一下,在一个空白html引入Lily.js, myLogger.js, 写入一些信息。

【写一个自己的js库】 2.实现自己的调试日志的更多相关文章
- 【写一个自己的js库】 1.搭个架子先
最近在看<javascript dom 高级程序设计>,想着跟着里面的代码敲一遍吧,也算是做一下学习笔记吧,所以这不是重新发明轮子,只是个学习的过程. 1.先确定自己的命名空间,并且加入几 ...
- 【写一个自己的js库】 5.添加修改样式的方法
1.根据id或class或tag修改样式,样式名是-连接格式的. function setStyleById(elem, styles){ if(!(elem = $(elem)) return fa ...
- 【写一个自己的js库】 3.添加几个处理字符串的方法
1.生成重复的字符串 if(!String.repeat){ String.prototype.repeat = function (count){ return new Array(count + ...
- 【写一个自己的js库】 4.完善跨浏览器事件操作
1.阻止冒泡. function stopPropagation(event){ event = event || getEvent(event); if(event.stopPropagation) ...
- 仿照jquery封装一个自己的js库(二)
本篇为完结篇.主要讲述如何造出轮子的高级特性. 一. css方法的高级操作 先看本文第一部分所讲的dQuery css方法 //css方法 dQuery.prototype.css=function( ...
- 仿照jquery封装一个自己的js库(一)
所谓造轮子的好处就是复习知识点,加深对原版jquery的理解. 本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包 ...
- 仿照jquery封装一个自己的js库
所谓造轮子的好处就是复习知识点,加深对原版jquery的理解.本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包括 ...
- 自己动手写一个iOS 网络请求库的三部曲[转]
代码示例:https://github.com/johnlui/Swift-On-iOS/blob/master/BuildYourHTTPRequestLibrary 开源项目:Pitaya,适合大 ...
- 如何写一个自定义的js文件
自定义一个Utils.js文件,在其中写js代码即可.如: (function(w){ function Utils(){} Utils.prototype.getChilds = function( ...
随机推荐
- Database Go and JSON
在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding/json这两个库能帮我 ...
- NASM mode for Emacs
NASM mode for Emacs Quick post for those Emacs users out there. The common assembler used on GNU ...
- 在 .NET Framework 2.0上使用LINQ
附件:System.Linq.dll.7z 此为从System.Core.dll中剥离的Linq,含有System.Linq.Enumerable类所有扩展方法,可以在客户只安装了.Net 2.0的环 ...
- GitHub好站点
https://github.com/XingCloud/stream_processor
- Mac Outlook数据文件的位置
****/Documents/Microsoft User Data/Office 2011 Identities/Main Identity 在这里 如果是中文版的,在这里: /Users/×××× ...
- ArcGIS 栅格数据已加载后的获取
原文 http://www.cnblogs.com/zoe-j/archive/2012/02/16/2354037.html 简单记一下,最近开始做Arcgis engine的开发, 已经通过了to ...
- Randomized QuickSelect
In this blog, we give a solution for Quick Select. Here, we have an improvement. The idea is to rand ...
- ZOJ3477&JAVA大数类
转:http://blog.csdn.net/sunkun2013/article/details/11822927 import java.util.*; import java.math.BigI ...
- 【v2.x OGE教程 20】粒子效果
1.介绍 粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术.而这些现象用其他传统的渲染技术难以实现的真实感的 game physics.常常使用粒子系统模拟的现象有火.爆炸.烟.水流.火花. ...
- Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Draw)过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8372924 在前面一篇文章中,我们分析了And ...