JavaScript的漂亮的代碼片段
動態(tài)構(gòu)建正則表達式
new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) )
來自sizzle,動態(tài)構(gòu)建正則時,這樣做避免了字符轉(zhuǎn)義。
更靈活和巧妙的數(shù)字補零
function prefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
取數(shù)組的最大和最小值
Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
產(chǎn)生漂亮的隨機字符串
Math.random().toString(16).substring(2); //8位
Math.random().toString(36).substring(2); //16位
獲取時間戳
相對于
var timeStamp = (new Date).getTime();
如下方式更方便:
var timeStamp = Number(new Date);
轉(zhuǎn)換為數(shù)值并取整
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
定義即運行函數(shù)
( function() {
// do something
} )();
這確實是最簡單的技巧,但也是最實用的技巧。 奠定了JavaScript封裝的基礎(chǔ)。
三元運算
var some = con1 ? val1 :
con2 ? val2 :
con3 ? val3 :
defaultVal;
一種函數(shù)注冊-調(diào)用機制
來自CKEditor,我做了提取。
( function() {
var fns = [];
// 將可用下標訪問屬性的對象轉(zhuǎn)換成數(shù)組
// 注意,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 ) );
}
};
} )();
// 應(yīng)用場景
var fnId;
// 在閉包中,添加一個可供全局調(diào)用的函數(shù)
( function() {
fnId = Util.addFunction( function( msg ) {
alert( msg );
} );
} )();
// 調(diào)用
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
相關(guān)文章
javascript getElementById 使用方法及用法
顧明思義,get-Element-By-Id,就是通過ID來設(shè)置/返回HTML標簽的屬性及調(diào)用其事件與方法。用這個方法基本上可以控制頁面所有標簽,條件很簡單就是給每個標簽分配一個ID號2008-11-11用JavaScript獲取頁面文檔內(nèi)容的實現(xiàn)代碼
下面小編就為大家?guī)硪黄肑avaScript獲取頁面文檔內(nèi)容的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06JavaScript里四舍五入函數(shù)round用法實例
這篇文章主要介紹了JavaScript里四舍五入函數(shù)round用法,實例分析了round函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04JavaScript實現(xiàn)防止網(wǎng)頁被嵌入Frame框架的代碼分享
這篇文章主要介紹了JavaScript實現(xiàn)防止網(wǎng)頁被嵌入Frame框架的代碼分享,本文給出了2種防嵌入方法,需要的朋友可以參考下2014-12-12