18 個 JavaScript 入門技巧!
1. 轉字符串
- const input = 123;
- console.log(input + ''); // '123'
- console.log(String(input)); // '123'
- console.log(input.toString()); // '123'
2. 轉數字
- const input = '123';
- console.log(+input); // 123
- console.log(Number(input)); // 123
- console.log(parseInt(input)); // 123
3.轉布爾值
- const input = 1;
- // 方案1 -使用雙感嘆號(!!)轉換為布爾值
- 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是一個值,而undefined不是一個值。null就像一個空盒子,而undefined沒有盒子。
- const fn = (x = '默認值') => console.log(x);
- fn(undefined); // 默認值
- fn(); // 默認值
- fn(null); // null
如果傳遞null,則不采用默認值,而傳遞undefined或不傳遞任何參數時,采用默認值。
6. 真值和虛值
虛值:false,0, "",null,undefined和NaN。
真值:"Values",0",{},[]。
7. const 聲明變量哪些類型可以被更改
如果值不想被改變時,可以使用 const:
- const name = '前端小智';
- name = '王大冶'; // 報錯
- const list = [];
- list = [1]; // 報錯
- const obj = {};
- obj = { name: '前端小智' }; // 報錯
但用 const 聲明的引用類型,它里面值是可以被更改的:
- const list = [];
- list.push(1); // 可以工作
- list[0] = 2; // 可以工作
- const obj = {};
- obj['name'] = '前端小智'; // 可以工作
8. 三等號和雙等號的區別
- // 雙等號 - 將兩個操作數轉換為相同類型,再比較
- console.log(0 == 'o'); // true
- // 三等號 - 不轉換為相同類型
- console.log(0 === '0'); // false
9. 接收參數更好的方式
- 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.把普通函數改成箭頭函數
- const func = function() {
- console.log('a');
- return 5;
- };
- func();
可以改寫成
- const func = () => (console.log('a'), 5);
- func();
11.從箭頭函數返回對象/表達式
- const getState = (name) => ({name, message: 'Hi'});
12. 將 set 轉換為數組
- 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
轉換為數組
- const arr = [...set]
13.檢查值是否為數組
- const arr = [1, 2, 3];
- console.log(typeof arr); // object
- console.log(Array.isArray(arr)); // true
14. 獲取對象的所有鍵
- cosnt obj = {
- name: "前端小智",
- age: 16,
- address: "廈門",
- profession: "前端開發",
- };
- console.log(Object.keys(obj)); // name, age, address, profession
15. 雙問號語法
- const height = 0;
- console.log(height || 100); // 100
- console.log(height ?? 100); // 0
這個 ?? 的意思是,如果 ?? 左邊的值是 null 或者 undefined,那么就返回右邊的值。
16. map()
map() 方法創建一個新數組,其結果是該數組中的每個元素是調用一次提供的函數后的返回值。
- 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); // 不管是否報錯,最后都會執行
- }
- };
- getData();
18. 解構
- 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
本文轉載自微信公眾號「大遷世界」,可以通過以下二維碼關注。轉載本文請聯系大遷世界公眾號。