17个你可能还不知道 JS 技巧!

开发 前端
17个你可能还不知道 JS 技巧!你知道吗?

 1.三元运算符

新手

let hungry = true
let eat;  
if (hungry == true) { 
       eat = 'yes';  
else { 
       eat = 'no'

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

老手

let hungry = true
let eat = hungry == true ? 'yes' : 'no'
  • 1.
  • 2.

2.数字转字符串/字符串转数字

新手

let num = 15;  
let s = num.toString(); // number to string 
let n = Number(s); // string to number 
  • 1.
  • 2.
  • 3.

老手

let num = 15; 
let s = num + ""; // 数字转字符串 
let n = +s; // 字符串转数字 
  • 1.
  • 2.
  • 3.

3.填充数组

新手

for(let i=0; i < arraySize; i++){ 
  filledArray[i] {'hello' : 'goodbye'}; 

  • 1.
  • 2.
  • 3.

老手

let filledArray = new Array(arraysize).fill(null).map(()=> ({'hello' : 'goodbye'})); 
  • 1.

4.对象的动态属性

新手

let dynamic = "value";  
let user = { 
     id: 1, 
}; 
user[dynamic] = "other value";  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

老手

let dynamic = "value";  
let user = { 
    id: 1, 
    [dynamic] = "other value" 
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

5.删除重复项

新手

let array = [100, 23, 23, 23, 23, 67, 45];  
let outputArray = []; 
let flag = false;  
for (j = 0; < array.length; j++) { 
   for (k = 0; k < outputArray.length; k++) { 
      if (array[j] == outputArray[k]) { 
         flag = true
       } 
    } 
    if (flag == false) { 
      outputArray.push(array[j]); 
     } 
     flag = false

// tArray = [100, 23, 67, 45] 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

老手

let array = [100, 23, 23, 23, 23, 67, 45];  
let outputArray = Array.from(new Set(array)) 
  • 1.
  • 2.

6. 数组到对象

新手

et arr = ["value1""value2""value3"]; 
 
let arrObject = {}; 
 
for (let i = 0; i < arr.length; ++i) { 
 
if (arr[i] !== undefined) { 
 
arrObject[i] = arr[i]; 
 

 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

老手

let arr = ["value1""value2""value3"];  
let arrObject = {...arr};  
  • 1.
  • 2.

7.对象到数组

新手

let number = { 
  one: 1,  
  two: 2, 
}; 
let keys = [];  
for (let numbers in numbers) { 
  if (number.hasOwnProperty(number)) { 
     keys.push(number); 
    } 

// key = [ 'one''two' ] 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

老手

let number = { 
  one: 1,  
  two: 2, 
}; 
let key = Object.keys(numbers); // key = [ 'one''two' ] 
let value = Object.values(numbers);  // value = [ 1, 2 ] 
let entry = Object.entries(numbers); // entry = [['one' : 1], ['two' : 2]] 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

8. 短路条件

新手

if (docs) { 
    goToDocs(); 

  • 1.
  • 2.
  • 3.

老手

docs && goToDocs() 
  • 1.

9. 使用^检查数字是否相等

if(a!=123) // before // 一般开发者 
 
if(a^123) // after // B格比较高的 
  • 1.
  • 2.
  • 3.

10.对象遍历

const age = { 
   Rahul: 20,   
   max: 16 
}; 
 
// 方案1:先得 key 在便利 key 
const keys = Object.keys(age);  
keys.forEach(key => age[key]++); 
 
console.log(age); // { Rahul: 21, max: 16 } 
 
// 方案2 - `for...in` 循环 
for(let key in age){ 
   age[key]++; 

 
console.log(age); // { Rahul: 22, max: 18 } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

11. 获取对象的所有键

cosnt obj = { 
  name"前端小智",  
  age: 16,  
  address: "厦门",  
  profession: "前端开发",  
};  
 
console.log(Object.keys(obj)); // name, age, address, profession 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

12.检查值是否为数组

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

13.初始化大小为n的数组并填充默认值

const size = 5; 
const defaultValue = 0; 
const arr = Array(size).fill(defaultValue); 
console.log(arr); // [0, 0, 0, 0, 0] 
  • 1.
  • 2.
  • 3.
  • 4.

14. 真值和虚值

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

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

15. 三等号和双等号的区别

// 双等号 - 将两个操作数转换为相同类型,再比较 
console.log(0 == 'o'); // true 
 
// 三等号 - 不转换为相同类型 
console.log(0 === '0'); // false 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

16. 接收参数更好的方式

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

更简单的方法

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

17.null vs undefined

null =>它是一个值,而undefined不是。

const fn = (x = 'default value') => console.log(x); 
 
fn(undefined); // default value 
fn(); // default value 
 
fn(null); // null 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

传递null时,不采用默认值,而 undefined或未传递任何内容时,将采用默认值。

作者:Rahul 译者:前端小智 来源:dev

原文:https://dev.to/rahxuls/17-javascript-tricks-you-didn-t-know-5gog

本文转载自微信公众号「大迁世界」,可以通过以下二维码关注。转载本文请联系大迁世界公众号。

 

责任编辑:武晓燕 来源: 大迁世界
相关推荐

2022-05-05 12:02:45

SCSS函数开发

2023-07-07 14:47:46

JavaScript技巧

2021-03-18 14:02:56

iOS苹果细节

2020-01-29 19:40:36

Python美好,一直在身边Line

2021-01-05 11:22:58

Python字符串代码

2015-08-13 09:03:14

调试技巧

2018-09-02 15:43:56

Python代码编程语言

2021-05-12 10:48:02

苹果技巧功能

2024-03-04 00:00:00

Kubernetes技巧API

2025-02-27 08:33:13

2021-10-22 09:41:26

桥接模式设计

2017-10-16 13:30:28

windows 10技巧输入法

2017-11-07 21:58:25

前端JavaScript调试技巧

2020-11-03 09:51:04

JavaScript开发 技巧

2023-04-09 23:37:31

JavaScript开发

2015-07-13 08:49:54

2020-10-28 08:06:09

Vue3框架数据

2020-04-10 16:10:17

微信小技巧移动应用

2016-07-22 17:55:07

云计算

2023-12-21 14:40:09

Python编程语言
点赞
收藏

51CTO技术栈公众号