首頁  >  文章  >  web前端  >  總結 18 個 JavaScript 入門技巧!

總結 18 個 JavaScript 入門技巧!

coldplay.xixi
coldplay.xixi轉載
2021-01-06 10:00:471878瀏覽

JavaScript專欄介紹18 個入門技巧

總結 18 個 JavaScript 入門技巧!

推薦(免費) :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');
  1. 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,undefinedNaN

真值:"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
這個

?? 的意思是,如果??# 左邊的值是nullundefined,那就回傳右邊的值。

##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'

以上是總結 18 個 JavaScript 入門技巧!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除