javascript一些小技巧

來源:互聯網
上載者:User
  • 字串轉換為數值

常規方法:

var var1 = parseInt("2");var var2 = parseFloat("2");var var3 = Number("2");
var var3 = new Number("2");

簡便方法:

var var1 = +("2");

另一簡便方法:

var str = "1.0";var num = str - 0;

 

  • 將其他類型轉換為boolean類型

在JavaScript中,所有值都能隱式的轉化為Boolean類型:

資料類型 轉換為true的值 轉換為false的值
Boolean true false
String 任何非Null 字元串 ""(Null 字元串)
Number 任何非零數字值(包括無窮大) 0、NaN
Object 任何對象 null
Undefined (不適用) undefined

舉例:

0 == false; // true1 == true; // true'' == false // truenull == false // true

我們也可以顯示轉化為Boolean類型:

var a = Boolean("Hello"); //true

更簡單的方法:

var a = "Hello";var b = !!a;

 

  • 建立多維陣列

一般方法:

var arr = new Array(2);arr[0] = new Array(2);arr[1] = new Array(2);arr[0][0] = 1;arr[0][1] = 2;arr[1][0] = 3;arr[1][1] = 4;

簡便方法:

var arr = {};arr[[0, 0]] = 1;arr[[0, 1]] = 2;arr[[1, 0]] = 3;arr[[1, 1]] = 4;

其他方法1(推薦):

var arr = [];arr[0] = [1, 2];arr[1] = [3, 4];

 其他方法2(樓下補充,推薦):

var arr=[        [1, 2],        [3, 4]];    

 

 

  • 阻止別人在iframe中載入你的頁面

防止把你的網頁通過iframe嵌入它自己的網頁:

if(top !== window) {   top.location.href = window.location.href;}

這段代碼應該放在每個頁面的head中。

 

  • 將arguments參數對象轉換為數組

在JavaScript中,函數中的預定義變數arguments並非一個真正的數組,而是一個類似數組的對象。 它具有length屬性,但是沒有數組對象的slice, push, sort等函數,而這些有時我們經常在函數裡用到,所以我們需要把參數轉換為真正的數組:

function func() {    var arr = Array.prototype.slice.call(arguments, 0);    return arr;}

 

  • 遍曆得到的正則結果

常規方法:

var str = "132ada5d6g3j";var match = str.match(/\d/g, str);var arr = [];var j;for(var i = 0, j = match.length; i < j; i++) {    arr.push(match[i]);}console.log(arr);

快捷方法:

var str = "132ada5d6g3j"; 
var arr = [];
str.replace(/\d/g, function() {
arr.push(arguments[0]);
});
console.log(arr);

另一快捷方法:

var str = "132ada5d6g3j"; 
var arr = str.replace(/\D/g,' ').split(' ');
console.log(arr);

 

  • 擷取數字數組中的最大值

常規方法:

var arr = [1, 5, 4, 12355, 43, 123, 123, 3, 4454, 43];var max = arr[0];for(var i in arr) {    if(arr[i] > max) {        max = arr[i];    }}/* 迴圈也可能是:for(var i = 0 ,j = arr.length; i < j; i++) {    if(arr[i] > max) {        max = arr[i];    }}*/console.log(max);

簡便方法:

var arr = [1, 5, 4, 12355, 43, 123, 123, 3, 4454, 43];var max = Math.max.apply(null, arr);console.log(max);

 

  • 數字取整

常規方法:

var num = 5.63;console.log(Math.floor(num));

其他方法1(只可用於不小於0的數):

var num = 5.63;console.log(num>>>0);

其他方法2(正負數都可用,推薦):

var num = 5.63;console.log(~~num);

 

  • 修正類似於0.1+0.2 != 0.3的錯誤

在JavaScript中,數字是基於IEEE754的數值,所以會出現浮點運算誤差的情況,這是使用IEEE754的通病,不是語言本身的問題:

var num = 0.1 + 0.2; // 0.30000000000000004console.log(num == 0.3); // false

修正方法:

var num = 0.1 + 0.2; // 0.30000000000000004console.log(num);/*你可以通過toFixed方法指定四捨五入的小數位元:*/console.log(num.toFixed()); // "0"console.log(num.toFixed(1)); // "0.3"

 

  • 整數前補0

普通方法:

 /*注意,這裡的n表示數字num補0後的位元*/function addZero(num, n) {  var len = num.toString().length;  while(len < n){    num = "0" + num;    len++;  }  return num;}console.log(addZero(5,8)); //00000005

簡單方法:

function addZero(num, n) {   y='00000000000000000000000000000'+num; /*這裡0的數目可調整*/   return y.substr(y.length-n);}console.log(addZero(5,8)); //00000005

-------------------------------------------------------------------

字串串連的效能問題應該很經典了,就不詳細介紹了。

歡迎大家補充!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.