您的位置:首页 > Web前端 > JavaScript

JavaScript常用脚本集锦6

2015-01-10 00:14 375 查看

JavaScript常用脚本集锦6

2015-01-10 00:14
40人阅读 评论(0)
收藏
举报

清楚节点内的空格

function cleanWhitespace(element) {
//如果不提供参数,则处理整个HTML文档
element = element || document;
//使用第一个节点作为开始指针
var cur = element.firstChild;
//一直循环,直到没有子节点为止。
while (cur != null) {
//如果节点是文本节点,并且只包含空格
if ((cur.nodeType == 3) && !/\S/.test(cur.nodeValue)) {
element.removeChild(cur);
}
//一个节点元素
else if (cur.nodeType == 1) {
//递归整个文档
cleanWhitespace(cur);
}
cur = cur.nextSibling;  //遍历子节点
}
}

代码来源:https://gist.github.com/hehongwei44/9105deee7b9bde88463b

JavaScript中的类继承实现方式

/**
* 把一个实例方法添加到一个类中
* 这个将会添加一个公共方法到 Function.prototype中,
* 这样通过类扩展所有的函数都可以用它了。它要一个名称和一个函数作为参数。
* 它返回 this。当我写一个没有返回值的方法时,我通常都会让它返回this。
* 这样可以形成链式语句。
*
* */
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
/**
* 它会指出一个类是继承自另一个类的。
* 它必须在两个类都定义完了之后才能定义,但要在方法继承之前调用。
*
* */
Function.method('inherits', function (parent) {
var d = 0, p = (this.prototype = new parent());

this.method('uber', function uber(name) {
var f, r, t = d, v = parent.prototype;
if (t) {
while (t) {
v = v.constructor.prototype;
t -= 1;
}
f = v[name];
} else {
f = p[name];
if (f == this[name]) {
f = v[name];
}
}
d += 1;
r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d -= 1;
return r;
});
return this;
});
/**
*
* The swiss方法对每个参数进行循环。每个名称,
* 它都将parent的原型中的成员复制下来到新的类的prototype中
*
* */
Function.method('swiss', function (parent) {
for (var i = 1; i < arguments.length; i += 1) {
var name = arguments[i];
this.prototype[name] = parent.prototype[name];
}
return this;
});

代码来源:https://gist.github.com/hehongwei44/2f89e61a0e6d4fd722c4

将单个字符串的首字母大写

/**
*
* 将单个字符串的首字母大写
*
*/
var fistLetterUpper = function(str) {
return str.charAt(0).toUpperCase()+str.slice(1);
};
console.log(fistLetterUpper('hello'));      //Hello
console.log(fistLetterUpper('good'));       //Good

代码来源:https://gist.github.com/hehongwei44/7e879f795226316c260d

变量的类型检查方式

/**
*
* js的类型检测方式->typeof、constuctor。
* 推荐通过构造函数来检测变量的类型。
*/
var obj = {key:'value'},
arr = ["hello","javascript"],
fn  = function(){},
str = "hello js",
num = 55,
bool = true,
User = function(){},
user = new User();
/*typeof测试*/
console.log(typeof obj);    //obj
console.log(typeof arr);    //obj
console.log(typeof fn);     //function
console.log(typeof str);    //string
console.log(typeof num);    //number
console.log(typeof bool);   //boolean
console.log(typeof user);   //object
/*constructor测试*/
console.log(obj.constructor == Object); //true
console.log(arr.constructor == Array);  //true
console.log(str.constructor == String); //true
console.log(num.constructor == Number); //true
console.log(bool.constructor == Boolean);//true
console.log(user.constructor == User);  //true

代码来源:https://gist.github.com/hehongwei44/1d808ca9b7c67745f689

页面倒计时的一段运用

/**
*
* @descition: 倒计时的一段脚本。
* @param:deadline ->截止日期 符合日期格式,比如2012-2-1 2012/2/1等有效日期。
* @return -> 截止的天数、小时、分钟、秒数组成的object对象。
*/
function getCountDown(deadline) {
var activeDateObj = {},
currentDate  = new Date().getTime(),            //获取当前的时间
finalDate    = new Date(deadline).getTime(),    //获取截止日期
intervaltime = finalDate - currentDate;         //有效期时间戳

/*截止日期到期的话,则不执行下面的逻辑*/
if(intervaltime < 0) {
return;
}

var totalSecond = ~~(intervaltime / 1000),     //得到秒数
toDay       = ~~(totalSecond / 86400 ),   //得到天数
toHour      = ~~((totalSecond -  toDay * 86400) / 3600), //得到小时
tominute    = ~~((totalSecond -  toDay * 86400 - toHour * 3600) / 60), //得到分数
toSeconde   = ~~(totalSecond - toDay * 86400 - toHour * 3600 -tominute * 60);

/*装配obj*/
activeDateObj.day    = toDay;
activeDateObj.hour   = toHour;
activeDateObj.minute = tominute;
activeDateObj.second = toSeconde;

return activeDateObj;
}

代码来源:https://gist.github.com/hehongwei44/a1205e9ff17cfc2359ca

 
 
上一篇Javascript 设计模式 -- Module(模块)模式
下一篇如何快速上手 IntelliJ IDEa
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: