18 個(gè) JavaScript 入門技巧!
1. 轉(zhuǎn)字符串
- const input = 123;
 - console.log(input + ''); // '123'
 - console.log(String(input)); // '123'
 - console.log(input.toString()); // '123'
 
2. 轉(zhuǎn)數(shù)字
- const input = '123';
 - console.log(+input); // 123
 - console.log(Number(input)); // 123
 - console.log(parseInt(input)); // 123
 
3.轉(zhuǎn)布爾值
- const input = 1;
 - // 方案1 -使用雙感嘆號(!!)轉(zhuǎn)換為布爾值
 - console.log(!!input); // true
 - // 方案2 - 使用 Boolean() 方法
 - console.log(Boolean(input)); // true
 
4.字符串'false'有問題
- const value = 'false';
 - console.log(Boolean(value)); // true
 - console.log(!!value); // true
 - // 最好的檢查方法
 - console.log(value === 'false');
 
5.null vs undefined
null是一個(gè)值,而undefined不是一個(gè)值。null就像一個(gè)空盒子,而undefined沒有盒子。
- const fn = (x = '默認(rèn)值') => console.log(x);
 - fn(undefined); // 默認(rèn)值
 - fn(); // 默認(rèn)值
 - fn(null); // null
 
如果傳遞null,則不采用默認(rèn)值,而傳遞undefined或不傳遞任何參數(shù)時(shí),采用默認(rèn)值。
6. 真值和虛值
虛值:false,0, "",null,undefined和NaN。
真值:"Values",0",{},[]。
7. const 聲明變量哪些類型可以被更改
如果值不想被改變時(shí),可以使用 const:
- const name = '前端小智';
 - name = '王大冶'; // 報(bào)錯(cuò)
 - const list = [];
 - list = [1]; // 報(bào)錯(cuò)
 - const obj = {};
 - obj = { name: '前端小智' }; // 報(bào)錯(cuò)
 
但用 const 聲明的引用類型,它里面值是可以被更改的:
- const list = [];
 - list.push(1); // 可以工作
 - list[0] = 2; // 可以工作
 - const obj = {};
 - obj['name'] = '前端小智'; // 可以工作
 
8. 三等號和雙等號的區(qū)別
- // 雙等號 - 將兩個(gè)操作數(shù)轉(zhuǎn)換為相同類型,再比較
 - console.log(0 == 'o'); // true
 - // 三等號 - 不轉(zhuǎn)換為相同類型
 - console.log(0 === '0'); // false
 
9. 接收參數(shù)更好的方式
- function downloadData(url, resourceId, searchTest, pageNo, limit) {}
 - downloadData(...); // need to remember the order
 
更簡單的方法
- function downloadData(
 - { url, resourceId, searchTest, pageNo, limit } = {}
 - ) {}
 - downloadData(
 - { resourceId: 2, url: "/posts", searchText: "WebDev" }
 - );
 
10.把普通函數(shù)改成箭頭函數(shù)
- const func = function() {
 - console.log('a');
 - return 5;
 - };
 - func();
 
可以改寫成
- const func = () => (console.log('a'), 5);
 - func();
 
11.從箭頭函數(shù)返回對象/表達(dá)式
- const getState = (name) => ({name, message: 'Hi'});
 
12. 將 set 轉(zhuǎn)換為數(shù)組
- const set = new Set([1, 2, 1, 4, 5, 6, 7, 1, 2, 4]);
 - console.log(set); // Set(6) {1, 2, 4, 5, 6, 7}
 - set.map((num) => num * num); // TypeError: set.map is not a function
 
轉(zhuǎn)換為數(shù)組
- const arr = [...set]
 
13.檢查值是否為數(shù)組
- const arr = [1, 2, 3];
 - console.log(typeof arr); // object
 - console.log(Array.isArray(arr)); // true
 
14. 獲取對象的所有鍵
- cosnt obj = {
 - name: "前端小智",
 - age: 16,
 - address: "廈門",
 - profession: "前端開發(fā)",
 - };
 - console.log(Object.keys(obj)); // name, age, address, profession
 
15. 雙問號語法
- const height = 0;
 - console.log(height || 100); // 100
 - console.log(height ?? 100); // 0
 
這個(gè) ?? 的意思是,如果 ?? 左邊的值是 null 或者 undefined,那么就返回右邊的值。
16. map()
map() 方法創(chuàng)建一個(gè)新數(shù)組,其結(jié)果是該數(shù)組中的每個(gè)元素是調(diào)用一次提供的函數(shù)后的返回值。
- const numList = [1, 2, 3];
 - const square = (num) => {
 - return num * num
 - }
 - const squares = numList.map(square);
 - console.log(squares); // [1, 4, 9]
 
17. try..catch..finally
- const getData = async () => {
 - try {
 - setLoading(true);
 - const response = await fetch(
 - "https://jsonplaceholder.typicode.com/posts"
 - );
 - const data = await response.json();
 - setData(data);
 - } catch (error) {
 - console.log(error);
 - setToastMessage(error);
 - } finally {
 - setLoading(false); // 不管是否報(bào)錯(cuò),最后都會執(zhí)行
 - }
 - };
 - getData();
 
18. 解構(gòu)
- const response = {
 - msg: "success",
 - tags: ["programming", "javascript", "computer"],
 - body: {
 - count: 5
 - },
 - };
 - const {
 - body: {
 - count,
 - unknownProperty = 'test'
 - },
 - } = response;
 - console.log(count, unknownProperty); // 5 'test'
 
作者:Mehul Lakhanpal 譯者:前端小智 來源:dev
原文:https://dev.to/318097/18-tips-for-junior-javascript-developer-57oa
本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。
















 
 
 














 
 
 
 