JS小知識(shí),分享十個(gè)有用 JavaScript 小技巧
您可能已經(jīng)知道 JavaScript 是世界上使用最廣泛的編程語言。它用于 Web、移動(dòng)混合應(yīng)用程序、服務(wù)器端 (NodeJS) 和各種其他應(yīng)用程序。由于它可用于在 Web 瀏覽器中顯示以及使用 nodebot 或其他智能交互機(jī)器人,因此它可以作為許多新開發(fā)人員的編程入門。在就業(yè)市場上,精通 JavaScript 并能編寫干凈、高效代碼的開發(fā)人員需求量很大。
無論使用何種瀏覽器/引擎或 SSJS(Server Side JavaScript)解釋器,所有 JavaScript 開發(fā)人員都應(yīng)該熟悉我將在本文中分享的提示、技巧和最佳實(shí)踐。
1、一直使用 === 替代 ==
如有必要,使用 ==(或!=)運(yùn)算符自動(dòng)執(zhí)行類型轉(zhuǎn)換。使用 ===(或 !==)運(yùn)算符時(shí)不會(huì)進(jìn)行轉(zhuǎn)換。有人可能會(huì)爭辯說它比較值和類型比 == 更快。
[5] === 5 // is false
[5] == 5 // is true
'5' == 5 // is true
'5' === 5 // is false
[] == 0 // is true
[] === 0 // is false
'' == false // is true but true == "a" is false
'' === false // is false
2、第一次聲明變量的值時(shí),盡量避免使用 var 關(guān)鍵字
全局變量在分配給未聲明的變量時(shí)自動(dòng)聲明。盡量防止使用全局變量。
3、使用 typeof、instanceof 和 constructor 時(shí)要小心。
typeof 操作符用于檢測變量的數(shù)據(jù)類型,它返回一個(gè)字符串,表示變量的類型。它可以檢測出所有類型,包括:
- 基本類型(number, string, boolean, undefined)
- 引用類型(object, function)
- 如果要檢測一個(gè)變量是否是基本類型,可以使用 typeof。
let x = 5;
console.log(typeof x); // "number"
instanceof 操作符用于檢測一個(gè)對象是否是某個(gè)類的實(shí)例。它返回一個(gè)布爾值,表示對象是否是類的實(shí)例。它只能用于檢測對象類型,因?yàn)榛绢愋蜎]有構(gòu)造函數(shù)。
let x = new Date();
console.log(x instanceof Date); // true
constructor 屬性返回創(chuàng)建對象的構(gòu)造函數(shù)。它也只能用于檢測對象類型。
let x = new Date();
console.log(x.constructor === Date); // true
它們之間的區(qū)別就是 typeof 操作符用于檢測類型,而 instanceof 和 constructor 都用于檢測類。 instanceof 操作符檢測對象是否是某個(gè)類的實(shí)例,而 constructor 檢測對象是由哪個(gè)類創(chuàng)建的。
另外,typeof 可以檢測出所有類型,而 instanceof 和 constructor 只能檢測對象
還有一點(diǎn)值得注意的是,instanceof 操作符在檢測繼承關(guān)系時(shí)也是有效的。比如,如果一個(gè)類 B 繼承自另一個(gè)類 A,那么一個(gè) B 類的實(shí)例也是 A 類的實(shí)例。
class A {}
class B extends A {}
let x = new B();
console.log(x instanceof B); // true
console.log(x instanceof A); // true
而 constructor 屬性則只能檢測到對象是由哪個(gè)類直接創(chuàng)建的,并不能檢測繼承關(guān)系。
class A {}
class B extends A {}
let x = new B();
console.log(x.constructor === B); // true
console.log(x.constructor === A); // false
總結(jié)一下,typeof 操作符用于檢測變量的類型,instanceof 操作符用于檢測對象是否是某個(gè)類的實(shí)例,而 constructor 屬性用于檢測對象是由哪個(gè)類創(chuàng)建的。
typeof 和 instanceof 都只能檢測對象類型,但instanceof 不能檢測基本類型。 instanceof 可以檢測繼承關(guān)系,而 constructor 只能檢測直接創(chuàng)建的關(guān)系。
4、False 值包括 undefined、null、0、false、NaN 和“(空字符串)
5、創(chuàng)建自調(diào)用函數(shù)(IIFE)
自調(diào)用匿名函數(shù)或立即調(diào)用函數(shù)表達(dá)式是此 (IIFE) 的通用名稱。它是以下形式的函數(shù),在創(chuàng)建后立即運(yùn)行:
(function(){
// some private code to be executed automatically
})();
(function(d,y){
var result = d+y;
return result;
})(20,20)
6、隨機(jī)數(shù)字?jǐn)?shù)組
var numbers = [6, 650, 140 , -225 , 233 , 500 , 152300, -81451];
numbers = numbers.sort(function(){ return Math.random() - 0.5});
7、驗(yàn)證給定的參數(shù)是一個(gè)數(shù)字
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
8、將 arguments 對象轉(zhuǎn)換為數(shù)組
var argumentsArray = Array.prototype.slice.call(arguments);
9、清空數(shù)組
var newArray = [12 , 222 , 1000 ];
newArray.length = 0; // newArray will be equal to [].
10、使用 map() 函數(shù)方法循環(huán)遍歷數(shù)組的項(xiàng)目
var squares = [1,4,6,2].map(function (val) {
return val * val;
});
// squares will be equal to [1, 16, 36, 4]
結(jié)束語
由于文章篇幅問題,今天的介紹就到這里。