给 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
Nullish coalescing operator
(??)仅当左侧值为undefined
或时才返回右侧值null
5. 将数字从十进制转换为二进制
toString()
可用于将数字转换为不同的进制。它接受一个参数,指定要转换的进制。
要将数字转换为二进制,进制为2
。
const decimal = 5;
const binary = decimal.toString(2);
console.log(binary); // 101
6. 为函数添加属性
function greetings() {
console.log("hello world");
greetings.counter++;
}
greetings.counter = 0;
greetings();
greetings();
console.log(`Called ${greetings.counter} times`); // Called 2 times
7. 使用 length 属性更改数组大小
const arr = [1, 2, 3, 4, 5];
arr.length = 2;
console.log(arr); // [1, 2]
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
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
*/
感谢阅读💙
关注@codedrops.tech获取每日更新
。Instagram ● Twitter ● Facebook
微学习 ● Web 开发 ● Javascript ● MERN 堆栈 ● Javascript
codedrops.tech