[Javascript] Highlights from IO18 Javascript new features
JavaScript from New Features
2023-09-14 09:00:50 时间
Latest Javascript features, not supported by all broswers, but can use with babel.
# try-catch-finally, Promise finally:
async function fetchAndDisplay({url, element}) { showLoadingSpinner(); try { const response = await fetch(url); const text = await response.text(); element.textContent = text; } catch(error) { element.textContent = error.message; } finally { hideLoadingSpinner(); } }
function getStarsNumber(username, reponame) { startLoadingAnimation(); return fetch(`https://api.github.com/repos/${username}/${reponame}`) .then(res => res.json()) .then(data => data.stargazers_count) .catch(() => `Couldn't get the stars number`) .finally(stopLoadingAnimation); }
# Regex:
1. Lookbehind:
// Positive lookbehind: const partten = /(?<=\$)\d+/u; //looking for any number after $ const res = pattern.exec('$42'); // res[0] === '42' // Negative lookbehind: const pattern = /(?>!\$)\d+/u; // looking for any number which is not after $ const res = pattern.exec('£42'); // res[0] === '42'
2. Lookahead:
// Positive lookahead: const pattern = /\d+(?= dollars)/u; // looking for any number before dollars const res = pattern.exec('42 dollars'); // res[0] === '42' // Negative lookahead: const pattern = /\d+(?! dollars)/u; // looking for any number which is not before dollars const res = pattern.exec('42 rupees'); // res[0] === '42'
3. Named group catch:
const pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u; const res = pattern.exec('2018-05-09');; // res.group.year === '2018' // res.group.month === '05' // res.group.day === '09'
4. Find whole words with line break:
using: 's', pretty much you can add 'us' to all regex.
# Javascript module:
<link rel = "modulepreload" href="lib.mjs"> <!-- preload module --> <link rel = "modulepreload" href="main.mjs"> <script type="module" src="main.mjs"></script> <!-- javascript module file--> <script nomodule src="fallback.js"></script> <!-- fallback to normal js file -->
相关文章
- LINQ to JavaScript
- JavaScript中valueOf函数
- [Javascript] Create 2d array by using Array.from
- [Javascript] Build lodash.merge from Scratch
- [Javascript] Filter out Duplicates from Flat JavaScript Array with array.filter / reduce / Set
- [Javascript] Broadcaster + Operator + Listener pattern -- 7. Create a broadcaster forOf
- [Javascript] 4 ways to remove duplicates elements from an array with Javascript
- [Javascript] Use a Pure RNG with the State ADT to Select an Element from State
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
- [Javascript Crocks] Recover from a Nothing with the `alt` method
- [WASM] Write to WebAssembly Memory from JavaScript
- [WASM] Read WebAssembly Memory from JavaScript
- [Javascript] Creating an Iterator from an Array
- [Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript
- Javascript 学习 笔记一
- resource handler working logic for Fiori BSP application - how is javascript file loaded from BSP re
- 150行JavaScript代码实现增强现实
- 使用Chrome开发者工具研究JavaScript的垃圾回收机制
- 使用JavaScript的FormData向SAP ABAP系统发起登录请求
- JavaScript数组的定义和用法知识点总结
- 《你不知道的JavaScript》第一部分:作用域和闭包
- JavaScript 变量
- JavaScript 实现阻碍调试