做者:Mehul Lakhanpal
译者:前端小智
来源:dev
点赞再看,微信搜索
【大迁世界】关注这个没有大厂背景,但有着一股向上积极心态人。本文
GitHub
https://github.com/qq44924588... 上已经收录,文章的已分类,也整理了不少个人文档,和教程资料。**
最近开源了一个 Vue 组件,还不够完善,欢迎你们来一块儿完善它,也但愿你们能给个 star 支持一下,谢谢各位了。javascript
github 地址:https://github.com/qq44924588...前端
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
没有盒子。vue
const fn = (x = '默认值') => console.log(x); fn(undefined); // 默认值 fn(); // 默认值 fn(null); // null
若是传递null,则不采用默认值,而传递undefined
或不传递任何参数时,采用默认值。java
虚值:false
,0
, ""
,null
,undefined
和NaN
。git
真值:"Values"
,0"
,{}
,[]
。github
若是值不想被改变时,能够使用 const
:json
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();
能够改写成async
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。
原文:https://dev.to/318097/18-tips...
文章每周持续更新,能够微信搜索【大迁世界 】第一时间阅读,回复【福利】有多份前端视频等着你,本文 GitHub https://github.com/qq449245884/xiaozhi 已经收录,欢迎Star。