做者:Mehul Lakhanpal 译者:前端小智 来源:devjavascript
点赞再看,微信搜索**【大迁世界】关注这个没有大厂背景,但有着一股向上积极心态人。本文
GitHub
github.com/qq449245884… 上已经收录,文章的已分类,也整理了不少个人文档,和教程资料。前端
最近开源了一个 Vue 组件,还不够完善,欢迎你们来一块儿完善它,也但愿你们能给个 star 支持一下,谢谢各位了。vue
github 地址:github.com/qq449245884…java
const input = 123;
console.log(input + ''); // '123'
console.log(String(input)); // '123'
console.log(input.toString()); // '123'
复制代码
const input = '123';
console.log(+input); // 123
console.log(Number(input)); // 123
console.log(parseInt(input)); // 123
复制代码
const input = 1;
// 方案1 -使用双感叹号(!!)转换为布尔值
console.log(!!input); // true
// 方案2 - 使用 Boolean() 方法
console.log(Boolean(input)); // true
复制代码
'false'
有问题const value = 'false';
console.log(Boolean(value)); // true
console.log(!!value); // true
// 最好的检查方法
console.log(value === 'false');
复制代码
null
是一个值,而undefined
不是一个值。null
就像一个空盒子,而undefined
没有盒子。git
const fn = (x = '默认值') => console.log(x);
fn(undefined); // 默认值
fn(); // 默认值
fn(null); // null
复制代码
若是传递null,则不采用默认值,而传递undefined
或不传递任何参数时,采用默认值。github
虚值:false
,0
, ""
,null
,undefined
和NaN
。web
真值:"Values"
,0"
,{}
,[]
。json
若是值不想被改变时,能够使用 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'] = '前端小智'; // 能够工做
复制代码
// 双等号 - 将两个操做数转换为相同类型,再比较
console.log(0 == 'o'); // true
// 三等号 - 不转换为相同类型
console.log(0 === '0'); // false
复制代码
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" }
);
复制代码
const func = function() {
console.log('a');
return 5;
};
func();
复制代码
能够改写成
const func = () => (console.log('a'), 5);
func();
复制代码
const getState = (name) => ({name, message: 'Hi'});
复制代码
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]
复制代码
const arr = [1, 2, 3];
console.log(typeof arr); // object
console.log(Array.isArray(arr)); // true
复制代码
cosnt obj = {
name: "前端小智",
age: 16,
address: "厦门",
profession: "前端开发",
};
console.log(Object.keys(obj)); // name, age, address, profession
复制代码
const height = 0;
console.log(height || 100); // 100
console.log(height ?? 100); // 0
复制代码
这个 ??
的意思是,若是 ??
左边的值是 null
或者 undefined
,那么就返回右边的值。
map()
方法建立一个新数组,其结果是该数组中的每一个元素是调用一次提供的函数后的返回值。
const numList = [1, 2, 3];
const square = (num) => {
return num * num
}
const squares = numList.map(square);
console.log(squares); // [1, 4, 9]
复制代码
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();
复制代码
const response = {
msg: "success",
tags: ["programming", "javascript", "computer"],
body: {
count: 5
},
};
const {
body: {
count,
unknownProperty = 'test'
},
} = response;
console.log(count, unknownProperty); // 5 'test'
复制代码
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
文章每周持续更新,能够微信搜索**【大迁世界 】第一时间阅读,回复【福利】**有多份前端视频等着你,本文 GitHub github.com/qq449245884… 已经收录,欢迎Star。