作者 | 浪里行舟
责编 | 郭芮
let nestedProp = obj && obj.first && obj.first.second;
let nestedProp = obj?.first?.second;
let c = a ? a : b // 方式1let c = a || b // 方式2
let x = {profile: {name: '浪里行舟',age: ''}}console.log(x.profile.age || 18) //18
let c = a ?? b;// 等价于let c = a !== undefined && a !== null ? a : b;
const x = null;const y = x ?? 500;console.log(y); // 500const n = 0const m = n ?? 9000;console.log(m) // 0
const promises = [Promise.resolve(1),Promise.resolve(2),Promise.reject('error')];Promise.all(promises).then(responses => console.log(responses)).catch(e => console.log(e)) // "error"
Promise.allSettled([Promise.reject({ code: 500, msg: '服务异常' }),Promise.resolve({ code: 200, list: [] }),Promise.resolve({ code: 200, list: [] })]).then(res => {console.log(res)/*0: {status: "rejected", reason: {…}}1: {status: "fulfilled", value: {…}}2: {status: "fulfilled", value: {…}}*/// 过滤掉 rejected 状态,尽可能多的保证页面区域数据渲染RenderContent(res.filter(el => {return el.status !== 'rejected'}))})
function collectGroup1 (regExp, str) {const matches = []while (true) {const match = regExp.exec(str)if (match === null) breakmatches.push(match[1])}return matches}console.log(collectGroup1(/"([^"]*)"/g, `"foo" and "bar" and "baz"`))// [ 'foo', 'bar', 'baz' ]
function collectGroup1 (regExp, str) {let results = []for (const match of str.matchAll(regExp)) {results.push(match[1])}return results}console.log(collectGroup1(/"([^"]*)"/g, `"foo" and "bar" and "baz"`))// ["foo", "bar", "baz"]
el.onclick = () => {import('/modules/my-module.js').then(module => {// Do something with the module.}).catch(err => {// load error;})}
let module = await import('/modules/my-module.js');
var num = Number.MAX_SAFE_INTEGER; // -> 9007199254740991num = num + 1; // -> 9007199254740992// 再次加 +1 后无法正常运算num = num + 1; // -> 9007199254740992// 两个不同的值,却返回了true9007199254740992 === 9007199254740993 // -> true
const aNumber = 111;const aBigInt = BigInt(aNumber);aBigInt === 111n // truetypeof aBigInt === 'bigint' // truetypeof 111 // "number"typeof 111n // "bigint"
1234567890123456789n * 123n;// -> 151851850485185185047n
1n < 2// true1n + 2// Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
全局变量 window:是一个经典的获取全局对象的方法。但是它在 Node.js 和 Web Workers 中并不能使用。
全局变量 self:通常只在 Web Workers 和浏览器中生效。但是它不支持 Node.js。一些人会通过判断 self 是否存在识别代码是否运行在 Web Workers 和浏览器中。
全局变量 global:只在 Node.js 中生效。
// ES10之前的解决方案const getGlobal = function(){if(typeof self !== 'undefined') return selfif(typeof window !== 'undefined') return windowif(typeof global !== 'undefined') return globalthrow new Error('unable to locate global object')}// ES10内置globalThis.Array(0,1,2) // [0,1,2]// 定义一个全局对象v = { value:true } ,ES10用如下方式定义globalThis.v = { value:true }
// worker.jsglobalThis === self// node.jsglobalThis === global// browser.jsglobalThis === window
Object.prototype.isPrototypeOf(globalThis); // true
作者:浪里行舟,硕士研究生,专注于前端,运营有个人公众号前端工匠,致力于打造适合初中级工程师能够快速吸收的一系列优质文章。
今日福利:评论区留言入选,都可获得价值299元的「2020 AI开发者万人大会」在线直播门票一张。 快来动动手指,写下你想说的话吧
☞在非托管钱包中可能会出现价值3000万美元的BCH SIM 交换黑客攻击吗?
点击阅读原文,查看详情!