动态构建正则表达式
复制代码 代码如下:
new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) )
来自sizzle,动态构建正则时,这样做避免了字符转义。
更灵活和巧妙的数字补零
复制代码 代码如下:
function prefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
取数组的最大和最小值
复制代码 代码如下:
Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
产生漂亮的随机字符串
复制代码 代码如下:
Math.random().toString(16).substring(2); //8位
Math.random().toString(36).substring(2); //16位
获取时间戳
相对于
var timeStamp = (new Date).getTime();
如下方式更方便:
复制代码 代码如下:
var timeStamp = Number(new Date);
转换为数值并取整
复制代码 代码如下:
var result = '3.1415926' | 0; // 3
字符串格式化
复制代码 代码如下:
function format(format) {
if (!FB.String.format._formatRE) {
FB.String.format._formatRE = /(\{[^\}^\{]+\})/g;
}
var values = arguments;
return format.replace(
FB.String.format._formatRE,
function(str, m) {
var
index = parseInt(m.substr(1), 10),
value = values[index + 1];
if (value === null || value === undefined) {
return '';
}
return value.toString();
}
);
}
使用:
复制代码 代码如下:
format('{0}.facebook.com/{1}', 'www', 'login.php');
//-> www.facebook.com/login.php
交换两个变量的值
复制代码 代码如下:
var foo = 1;
var bar = 2;
foo = [bar, bar=foo][0];
RegExp Looping
复制代码 代码如下:
String.prototype.format = function ( /* args */ ) {
var args = arguments;
return this.replace(
/\{(\d+)\}/g,
function (full, idx) {
return args[idx];
} )
}
'Hello {0}, How{1}'.format( 'Bob', ' you doin');
// => Hello Bob, How you doinhttp://mazesoul.github.com/Readability_idioms_and_compression_tolerance/#31.0
定义即运行函数
复制代码 代码如下:
( function() {
// do something
} )();
这确实是最简单的技巧,但也是最实用的技巧。 奠定了JavaScript封装的基础。
三元运算
复制代码 代码如下:
var some = con1 ? val1 :
con2 ? val2 :
con3 ? val3 :
defaultVal;
一种函数注册-调用机制
来自CKEditor,我做了提取。
复制代码 代码如下:
( function() {
var fns = [];
// 将可用下标访问属性的对象转换成数组
// 注意,IE下DOMNodeList会失败
function toArray( arrayLike, index ) {
return Array.prototype.slice.call( arrayLike, index || 0 );
}
window.Util = {
'addFunction' : function( fn, scope ) {
return fns.push( function(){
return fn.apply( scope || window, arguments );
} ) - 1;
},
'removeFunction' : function( index ) {
fns[ index ] = null;
},
'callFunction' : function( index ) {
var fn = fns[ index ];
return fn && fn.apply( window, toArray( arguments, 1 ) );
}
};
} )();
// 应用场景
var fnId;
// 在闭包中,添加一个可供全局调用的函数
( function() {
fnId = Util.addFunction( function( msg ) {
alert( msg );
} );
} )();
// 调用
Util.callFunction( fnId, 'Hello, World' ); //-> 'Hello,World';
短路运算
复制代码 代码如下:
var something = 'xxxx';
console.log( true && something ); //-> 'xxx';
console.log( false && something ); //-> false
console.log( true || something ); // -> true
console.log( false || something ); //-> something
代码片段
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】