js 代码 摘录收集贴
不定期、偶尔、添加更新 在网络上看到的个人认为经典的代码
1.
//判断浏览器是否IE
if(!-[1,])
alert("IE");
else
alert("非IE");
2. [2012.1.6]
//数组 原型 函数被称为“第一型”经典例子 摘录自 http://www.51cto.com/art/200806/77576.htm
Array.prototype.each = function (closure) {
return this.length ? [closure(this.slice(0, 1))].concat(this.slice(1).each(closure)) : [];
//递归?
}
alert(([1, 2, 3, 4].each(function (x) { return x * 2 })).toString());
3. [2012.1.6]
//闭包 来源 http://book.51cto.com/art/200806/77577.htm
var r=(function outerFun(x){return function innerFun(y){return x * y}})(2)(3);
alert(r);
4. [判断是否为日期]-搜索来源:http://it.china-b.com/itwy/733458.html
String.prototype.isDate = function() {
var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null)
return false;
var d = new Date(r[1], r[3] - 1, r[4]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
};
String.prototype.isTime = function()
{
var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (r == null)
return false;
var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}