给 Web 开发者的 9 个实用技巧

2025-06-09

给 Web 开发者的 9 个实用技巧

1. 在 GitHub 仓库中搜索文件

t回购进入search mode项目的文件结构

替代文本

2. Github 中的高亮/回复快捷方式

  • 遇到问题时,突出显示需要回复的行。

替代文本

  • 然后按r回复评论

替代文本

3. 使用 Lodash 的快捷方式

  • 前往 Lodash 主页
  • 打开开发者工具
  • _Lodash 库可从变量中使用

替代文本

4. 空值合并运算符

const height = 0;
console.log(height || 100); // 100
console.log(height ?? 100); // 0
Enter fullscreen mode Exit fullscreen mode

Nullish coalescing operator(??)仅当左侧值为undefined或时才返回右侧值null

5. 将数字从十进制转换为二进制

toString()可用于将数字转换为不同的进制。它接受一个参数,指定要转换的进制。
要将数字转换为二进制,进制为2

const decimal = 5;
const binary = decimal.toString(2);
console.log(binary); // 101
Enter fullscreen mode Exit fullscreen mode

6. 为函数添加属性

function greetings() {
  console.log("hello world");
  greetings.counter++;
}
greetings.counter = 0;

greetings();
greetings();

console.log(`Called ${greetings.counter} times`); // Called 2 times
Enter fullscreen mode Exit fullscreen mode

7. 使用 length 属性更改数组大小

const arr = [1, 2, 3, 4, 5];
arr.length = 2;
console.log(arr); // [1, 2]
Enter fullscreen mode Exit fullscreen mode

8. 防止对象属性值更新

const obj = {name: 'Codedrops'};
console.log(obj.name); // Codedrops

/* Set the 'writable' descriptor to false for the 'name' key  */
Object.defineProperty(obj, 'name', {
        writable: false
});

obj.name = 'ABC';
console.log(obj.name); // Codedrops
Enter fullscreen mode Exit fullscreen mode

9. Map 可以存储任何类型的键

const myMap = new Map([]);

const numberKey = 1;
const stringKey = "str";
const arrayKey = [1, 2, 3];
const objectKey = { name: "abc" };

myMap.set(numberKey, "Number Key");
myMap.set(stringKey, "String Key");
myMap.set(arrayKey, "Array Key");
myMap.set(objectKey, "Object Key");

myMap.forEach((value, key) => console.log(`${key} : ${value}`));

/*
Output:
1 : Number Key
str : String Key
1,2,3 : Array Key
[object Object] : Object Key
*/
Enter fullscreen mode Exit fullscreen mode

感谢阅读💙

关注@codedrops.tech获取每日更新

。InstagramTwitterFacebook

微学习 ● Web 开发 ● Javascript ● MERN 堆栈 ● Javascript

codedrops.tech

鏂囩珷鏉ユ簮锛�https://dev.to/ml318097/9-cool-tips-tricks-for-web-developers-48m7
PREV
学习编程的 72 种免费方法(终极指南)
NEXT
给初级 JavaScript 开发者的 18 个技巧