随笔分类 - Javascript
摘要:In JavaScript, the factory pattern isn't much more than a function that returns an object without using the new keyword. ES6 arrow functions allow us
阅读全文
摘要:Source: https://javascriptpatterns.vercel.app/patterns/design-patterns/singleton-pattern With the Singleton Pattern, we restrict the instantiation of
阅读全文
摘要:Module pattern provide a way to have both public and private pieces with the export keyword. This protects values from leaking into the global scope o
阅读全文
摘要:Blog: https://www.geeksforgeeks.org/es6-trampoline-function/ Stackoverflow problem for recursion: const sumBelow = (number, sum = 0) => ( number 0 ? s
阅读全文
摘要:function lotteryNum() { return (Math.round(Math.random() * 100) % 58) + 1; } function recordNumber(luckLotteryNumbers: readonly number[], num: number)
阅读全文
摘要:String in Javascript is immutable. Means that once they were created, they cannot be modified. This also means that simple operations like appending a
阅读全文
摘要:function swap(array, i, j) { const [item] = array.splice(i, 1) // get item and remove this item from array array.splice(j, 0, item) }
阅读全文
摘要:Sometimes one line of code can eliminate 50% of your bundle size. As you'll see in this video, we can remove "dead code" from modules we are working w
阅读全文
摘要:This lessons builds on Build lodash.debounce from scratch to add support for one of its more interesting options: maxWait. The maxWait option ensures
阅读全文
摘要:This lesson will demonstrate how to recreate a simplified version of the popular lodash.debounce method from scratch. I literally failed a job intervi
阅读全文
摘要:This lesson will demonstrate how to recreate a simplified version of the popular lodash.merge method from scratch. First we'll create a test file with
阅读全文
摘要:// ❎ const object = {} object[key] = value // 👍 better performance const map = new Map() map.set(key, value)
阅读全文
摘要:const b = true const c = false let object = { a: 1, ...(b ? {b: 2}: {}), ...(c ? {c: 3}: {}) }; console.log(object) // { a: 1, b: 2 }
阅读全文
摘要:const getGlobalThis = () => { if (typeof self !== 'undefined') return self if (typeof window !== 'undefined') return window if (typeof global !== 'und
阅读全文
摘要:Add functionality to objects or classes without inheritance Although we can add functionality iwht mixins without inheritance, mixins themselves can u
阅读全文
摘要:For example, we have a dropdown list, we want to keep "other" option always as last option. return options .sort((a, b) => { if (a.value == b.value) r
阅读全文
摘要:Presnetational Component: A presentational compoentn receives its data through props. It's primary function is to simply display the data it receives
阅读全文
摘要:Make data available to multiple child cpmpopnents to avoid prop drilling Create a Context: Hooks We can create a hook to provide context to components
阅读全文
摘要:Share a single global insance throughout our application Proxies are a powerful way to add control over the behavior of an object. A proxy can have va
阅读全文
摘要:const players = [ { id: 'x9Opl1', name: 'Mario', bio: 'Italian plumber and lead character', }, { id: '7fGlZ0', name: 'Luigi', bio: "Mario's green youn
阅读全文