欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript的漂亮的代碼片段

 更新時間:2013年06月05日 12:47:33   作者:  
記錄我看到之后,不得不贊的代碼片段。本貼板內(nèi)容將不斷更新,歡迎關(guān)注

 動態(tài)構(gòu)建正則表達式

復(fù)制代碼 代碼如下:

 new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) )

來自sizzle,動態(tài)構(gòu)建正則時,這樣做避免了字符轉(zhuǎn)義。


更靈活和巧妙的數(shù)字補零

復(fù)制代碼 代碼如下:

function prefixInteger(num, length) {
    return (num / Math.pow(10, length)).toFixed(length).substr(2);
}

 取數(shù)組的最大和最小值

復(fù)制代碼 代碼如下:

Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1

產(chǎn)生漂亮的隨機字符串

復(fù)制代碼 代碼如下:

Math.random().toString(16).substring(2); //8位
Math.random().toString(36).substring(2); //16位


 獲取時間戳

相對于
var timeStamp = (new Date).getTime();
如下方式更方便:

復(fù)制代碼 代碼如下:

var timeStamp = Number(new Date);

 轉(zhuǎn)換為數(shù)值并取整

復(fù)制代碼 代碼如下:

var result = '3.1415926' | 0; // 3


字符串格式化

復(fù)制代碼 代碼如下:

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();
      }
    );
  }


  使用:
復(fù)制代碼 代碼如下:

format('{0}.facebook.com/{1}', 'www', 'login.php');
//-> www.facebook.com/login.php

交換兩個變量的值

復(fù)制代碼 代碼如下:

var foo = 1;
var bar = 2;
foo = [bar, bar=foo][0];

RegExp Looping

復(fù)制代碼 代碼如下:

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ù)

復(fù)制代碼 代碼如下:

( function() {
// do something
} )();

這確實是最簡單的技巧,但也是最實用的技巧。 奠定了JavaScript封裝的基礎(chǔ)。

三元運算

復(fù)制代碼 代碼如下:

var some = con1 ? val1 :
           con2 ? val2 :
           con3 ? val3 :
           defaultVal;

一種函數(shù)注冊-調(diào)用機制

來自CKEditor,我做了提取。

復(fù)制代碼 代碼如下:

( 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';

短路運算

復(fù)制代碼 代碼如下:

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)文章

最新評論