随笔分类 - Javascript
摘要:const ary = [1, 2, 3, 4, 2, 3]; const unqiAry = (ary) => ary.filter((item, index) => ary.indexOf(item) index) unqiAry(ary) // [ 1, 2, 3, 4 ] const ary
阅读全文
摘要:let other = null console.log(other?.[0]) // undefined You can use optional chaining to access elements in an array. If the array is null or undefined,
阅读全文
摘要:The Event Delegation Pattern Event delegation is a simple, but powerful leveraging of the DOM event system which allows for easier adding of functiona
阅读全文
摘要:Object.is console.log(Object.is(2, 2)); // true console.log(Object.is({}, {})); // false Strict Equality: a b console.log(2 2); // true console.log({}
阅读全文
摘要:removeEventListener removes an event listener added with addEventListener. However, there are a number of gotchas to watch out for in order to correct
阅读全文
摘要:const items = [ 'Sojourner', 'Opportunity', 'Spirit', 'Curiosity', 'Perseverance', ] const formatter = new Intl.ListFormat('en', { style: 'long', type
阅读全文
摘要:Objects have the ability to use data and methods that other objects contain, as long as it lives on the [[prototype]] chain. In this lesson we’ll test
阅读全文
摘要:let getUrl = url => listener => { let controller = new AbortController() let signal = controller.signal fetch(url, {signal}) .then((response) => { ret
阅读全文
摘要:While using async/await, seeing such error: Uncaught ReferenceError: regeneratorRuntime is not defined Solution: in package.json, add: "browserslist":
阅读全文
摘要:We want to only freeze the private variable when we get it and set it: class Cart { #items; constructor(items = []) { this.value = items; } set value(
阅读全文
摘要:Morden Javascript allows us to write private and static class memebers. To do this, we need to install babel plugins: First, let see "static member":
阅读全文
摘要:Let's see two code snippets which has same functionalities: No1: function Cart(items = []) { this.items = Object.freeze(items); this.add = item => { c
阅读全文
摘要:Install: npm i --save joi Example: const schema = Joi.object({ username: Joi.string() .alphanum() .min(3) .max(30) .required(), password: Joi.string()
阅读全文
摘要:const factorial = (n) => (n > 1 ? n * factorial(n - 1) : 1); const memoize = (fn) => { const cache = {}; return (...args) => { const key = JSON.string
阅读全文
摘要:In contrast to other built-ins, Arrays can be wrapped transparently: const p = new Proxy(new Array(), {}); p.push('a'); assert.equal(p.length, 1); p.l
阅读全文
摘要:Instances of most built-in constructors also use a mechanism that is not intercepted by Proxies. They therefore can’t be wrapped transparently, either
阅读全文
摘要:For a Proxy, function signature looks like: const target = {} const handler = { get(target, propKey) { return Reflect.get(target, propKey) } } const p
阅读全文
摘要:_.omitBy and its sibling _.pickBy are popular utilities which apply filter-like functionality to objects. Both of them accept a function to determine
阅读全文
摘要:Remove duplicates elements from an array is a common array task Javascript offers different alternatives to accomplish it. You can use a mix of method
阅读全文
摘要:When needing to format a number I've tended to lean towards Number.prototype.toFixed(), reach for a 3rd party library, or write custom functions. Howe
阅读全文