JS一些實用的方法

來源:互聯網
上載者:User

標籤:

1、首次為變數賦值時務必使用var關鍵字


變數沒有聲明而直接賦值得話,預設會作為一個新的全域變數,要盡量避免使用全域變數。




2、使用===取代==


==和!=操作符會在需要的情況下自動轉換資料類型。但===和!==不會,它們會同時比較值和資料類型,這也使得它們要比==和!=快。


[10] === 10    // is false
[10]  == 10    // is true
‘10‘ == 10     // is true
‘10‘ === 10    // is false
[]   == 0     // is true
[] ===  0     // is false
‘‘ == false   // is true but true == "a" is false
‘‘ === false  // is false




3、使用物件建構器


function Person(firstName, lastName){
    this.firstName =  firstName;
    this.lastName = lastName;
}
var Saad = new Person("Saad", "Mousliki");






4、從數組中隨機擷取成員


var items = [12, 548 , ‘a‘ , 2 , 5478 , ‘foo‘ , 8852, , ‘Doe‘ , 2145 , 119];
var  randomItem = items[Math.floor(Math.random() * items.length)];




5、擷取指定範圍內的隨機數


這個功能在產生測試用的假資料時特別有數,比如介與指定範圍內的工資數。


var x = Math.floor(Math.random() * (max - min + 1)) + min;






6、產生從0到指定值的數字數組


var numbersArray = [] , max = 100;
for( var i=1; numbersArray.push(i++) < max;);  // numbers = [1,2,3 ... 100]




7、產生隨機的字母數字字串


function generateRandomAlphaNum(len) {
    var rdmString = "";
    for( ; rdmString.length < len; rdmString  += Math.random().toString(36).substr(2));
    return  rdmString.substr(0, len);
}




8、數組之間追加


var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 值為  [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */




9、對象轉換為數組


var argArray = Array.prototype.slice.call(arguments);




10、擷取數組中的最大值和最小值


var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 
var maxInNumbers = Math.max.apply(Math, numbers); 
var minInNumbers = Math.min.apply(Math, numbers);




11、清空數組


var myArray = [12 , 222 , 1000 ];  
myArray.length = 0; // myArray will be equal to [].




12、不要直接從數組中delete或remove元素


如果對數組元素直接使用delete,其實並沒有刪除,只是將元素置為了undefined。數組元素刪除應使用splice。


切忌:


var items = [12, 548 ,‘a‘ , 2 , 5478 , ‘foo‘ , 8852, , ‘Doe‘ ,2154 , 119 ]; 
items.length; // return 11 
delete items[3]; // return true 
items.length; // return 11 
/* items 結果為 [12, 548, "a", undefined × 1, 5478, "foo", 8852, undefined × 1, "Doe", 2154, 119] */
而應:


var items = [12, 548 ,‘a‘ , 2 , 5478 , ‘foo‘ , 8852, , ‘Doe‘ ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items 結果為 [12, 548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154, 119]
刪除對象的屬性時可以使用delete。




13、使用length屬性截斷數組


前面的例子中用length屬性清空數組,同樣還可用它來截斷數組:


var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];  
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].
與此同時,如果把length屬性變大,數組的長度值變會增加,會使用undefined來作為新的元素填充。length是一個可寫的屬性。


myArray.length = 10; // the new array length is 10 
myArray[myArray.length - 1] ; // undefined




14、在條件中使用邏輯與或


var foo = 10;  
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething(); 
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();
邏輯或還可用來設定預設值,比如函數參數的預設值。


function doSomething(arg1){ 
    arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}




15、保留指定小數位元


var num =2.443242342;
num = num.toFixed(4);  // num will be equal to 2.4432
注意,toFixec()返回的是字串,不是數字。






16、浮點計算的問題


0.1 + 0.2 === 0.3 // is false 
9007199254740992 + 1 // is equal to 9007199254740992
9007199254740992 + 2 // is equal to 9007199254740994
為什麼呢?因為0.1+0.2等於0.30000000000000004。JavaScript的數字都遵循IEEE 754標準構建,在內部都是64位浮點小數表示,具體可以參見JavaScript中的數字是如何編碼的.


可以通過使用toFixed()和toPrecision()來解決這個問題。

JS一些實用的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.