自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

18 個(gè) JavaScript 入門技巧!

開發(fā) 前端
18 個(gè) JavaScript 入門技巧,你了解幾個(gè),下面我們一起聊聊!

[[355901]]

1. 轉(zhuǎn)字符串

 

  1. const input = 123; 
  2.  
  3. console.log(input + ''); // '123' 
  4. console.log(String(input)); // '123' 
  5. console.log(input.toString()); // '123' 

2. 轉(zhuǎn)數(shù)字

 

  1. const input = '123'
  2.  
  3. console.log(+input); // 123 
  4. console.log(Number(input)); // 123 
  5. console.log(parseInt(input)); // 123 

3.轉(zhuǎn)布爾值

 

  1. const input = 1; 
  2.  
  3. // 方案1 -使用雙感嘆號(hào)(!!)轉(zhuǎn)換為布爾值 
  4. console.log(!!input); // true 
  5.  
  6. // 方案2 - 使用 Boolean() 方法 
  7. console.log(Boolean(input)); // true 

4.字符串'false'有問題

 

  1. const value = 'false'
  2. console.log(Boolean(value)); // true 
  3. console.log(!!value); // true 
  4.  
  5. // 最好的檢查方法 
  6. console.log(value === 'false'); 

5.null vs undefined

null是一個(gè)值,而undefined不是一個(gè)值。null就像一個(gè)空盒子,而undefined沒有盒子。

 

  1. const fn = (x = '默認(rèn)值') => console.log(x); 
  2.  
  3. fn(undefined); // 默認(rèn)值 
  4. fn(); // 默認(rèn)值 
  5.  
  6. fn(null); // null 

如果傳遞null,則不采用默認(rèn)值,而傳遞undefined或不傳遞任何參數(shù)時(shí),采用默認(rèn)值。

6. 真值和虛值

虛值:false,0, "",null,undefined和NaN。

真值:"Values",0",{},[]。

7. const 聲明變量哪些類型可以被更改

如果值不想被改變時(shí),可以使用 const:

 

  1. const name = '前端小智'
  2. name = '王大冶'; // 報(bào)錯(cuò) 
  3.  
  4. const list = []; 
  5. list = [1]; // 報(bào)錯(cuò) 
  6.  
  7. const obj = {}; 
  8. obj = { name'前端小智' }; // 報(bào)錯(cuò) 

但用 const 聲明的引用類型,它里面值是可以被更改的:

 

  1. const list = []; 
  2. list.push(1); // 可以工作 
  3. list[0] = 2; // 可以工作 
  4.  
  5. const obj = {}; 
  6. obj['name'] = '前端小智'; // 可以工作 

8. 三等號(hào)和雙等號(hào)的區(qū)別

 

  1. // 雙等號(hào) - 將兩個(gè)操作數(shù)轉(zhuǎn)換為相同類型,再比較 
  2. console.log(0 == 'o'); // true 
  3.  
  4. // 三等號(hào) - 不轉(zhuǎn)換為相同類型 
  5. console.log(0 === '0'); // false 

9. 接收參數(shù)更好的方式

 

  1. function downloadData(url, resourceId, searchTest, pageNo, limit) {} 
  2.  
  3. downloadData(...); // need to remember the order 

更簡單的方法

 

  1. function downloadData( 
  2. { url, resourceId, searchTest, pageNo, limit } = {} 
  3. ) {} 
  4.  
  5. downloadData( 
  6.   { resourceId: 2, url: "/posts", searchText: "WebDev" } 
  7. ); 

10.把普通函數(shù)改成箭頭函數(shù)

 

  1. const func = function() { 
  2.     console.log('a'); 
  3.     return 5; 
  4. }; 
  5. func(); 

可以改寫成

 

  1. const func = () => (console.log('a'), 5); 
  2. func(); 

11.從箭頭函數(shù)返回對象/表達(dá)式

  1. const getState = (name) => ({name, message: 'Hi'}); 

12. 將 set 轉(zhuǎn)換為數(shù)組

 

  1. const set = new Set([1, 2, 1, 4, 5, 6, 7, 1, 2, 4]); 
  2. console.log(set); // Set(6) {1, 2, 4, 5, 6, 7} 
  3.  
  4. set.map((num) => num * num); // TypeError: set.map is not a function 

轉(zhuǎn)換為數(shù)組

  1. const arr = [...set

13.檢查值是否為數(shù)組

 

  1. const arr = [1, 2, 3];  
  2. console.log(typeof arr); // object 
  3. console.log(Array.isArray(arr)); // true 

14. 獲取對象的所有鍵

 

  1. cosnt obj = { 
  2.   name"前端小智",  
  3.   age: 16,  
  4.   address: "廈門",  
  5.   profession: "前端開發(fā)",  
  6. };  
  7.  
  8. console.log(Object.keys(obj)); // name, age, address, profession 

15. 雙問號(hào)語法

 

  1. const height = 0; 
  2.  
  3. console.log(height || 100); // 100 
  4. console.log(height ?? 100); // 0 

這個(gè) ?? 的意思是,如果 ?? 左邊的值是 null 或者 undefined,那么就返回右邊的值。

16. map()

map() 方法創(chuàng)建一個(gè)新數(shù)組,其結(jié)果是該數(shù)組中的每個(gè)元素是調(diào)用一次提供的函數(shù)后的返回值。

 

  1. const numList = [1, 2, 3]; 
  2.  
  3. const square = (num) => { 
  4.   return num * num 
  5.  
  6. const squares = numList.map(square); 
  7.  
  8. console.log(squares); // [1, 4, 9] 

17. try..catch..finally

 

  1. const getData = async () => { 
  2.   try { 
  3.     setLoading(true); 
  4.     const response = await fetch
  5.       "https://jsonplaceholder.typicode.com/posts" 
  6.     ); 
  7.     const data = await response.json(); 
  8.     setData(data); 
  9.   } catch (error) { 
  10.     console.log(error); 
  11.     setToastMessage(error); 
  12.   } finally { 
  13.     setLoading(false); // 不管是否報(bào)錯(cuò),最后都會(huì)執(zhí)行 
  14.   } 
  15. }; 
  16.  
  17. getData(); 

18. 解構(gòu)

 

  1. const response = { 
  2.   msg: "success"
  3.   tags: ["programming""javascript""computer"], 
  4.   body: { 
  5.     count: 5 
  6.   }, 
  7. }; 
  8.  
  9. const { 
  10.   body: { 
  11.     count
  12.         unknownProperty = 'test' 
  13.   }, 
  14. } = response; 
  15.  
  16. console.log(count, unknownProperty); // 5 'test' 

作者:Mehul Lakhanpal 譯者:前端小智 來源:dev

原文:https://dev.to/318097/18-tips-for-junior-javascript-developer-57oa

本文轉(zhuǎn)載自微信公眾號(hào)「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 大遷世界
相關(guān)推薦

2024-01-30 08:54:05

JavaScript技巧代碼

2020-09-29 08:14:46

JavaScript開發(fā)代碼

2023-03-06 10:42:34

CSS前端

2019-03-19 13:44:41

Python編程技巧編程語言

2022-02-24 10:05:20

Python編程語言代碼

2023-08-11 13:25:00

JavaScript

2024-06-21 11:02:16

2022-12-22 14:44:06

JavaScript技巧

2022-12-25 16:03:31

JavaScript技巧

2023-08-11 17:39:43

JavaScriptWeb 應(yīng)用程序

2020-08-21 17:40:15

JavaScript開發(fā) 技巧

2021-12-27 14:33:47

Python語言開發(fā)

2024-01-03 14:54:56

PythonPandas數(shù)據(jù)處理工具

2022-11-28 23:44:26

JavaScript技巧程序員

2023-11-26 17:54:07

JavaScript開發(fā)

2024-08-21 14:55:02

2019-10-10 14:48:19

深度學(xué)習(xí)人工智能

2023-07-18 07:56:31

工具reduce業(yè)務(wù)

2022-11-28 23:48:06

JavaScript編程語言技巧

2020-12-23 08:03:01

JavaScript開發(fā)代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)