随笔分类 - Javascript
摘要:sort(), mutates the original array, and return the reference to original array and sorted. The toSorted() method of Array instances is the copying ver
阅读全文
摘要:Array.prototype.splice()mutates the original array. To avoid mutation, we use Array.prototype.slice(). new method Array.prototype.toSpliced() return a
阅读全文
摘要:Prevously, when we want to upate an item inside a array: const items = [ {id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}, {id: 4, name: 'd'
阅读全文
摘要:Both JSON.parse(JSON,strigify())& structuredClone doesn't work with Dateand function. var foo = {name: 'foo', bar: () => 'bar'} var foo2 = structuredC
阅读全文
摘要:Compare two code snippet const people = [ {id: 1,name: 'John', age: 45}, {id: 2,name: "Op", age: 32}, {id: 3, name: "Wade",age: 39 } ] // option 1 con
阅读全文
摘要:// Initialize a 2D array with zeros const str1 = "Hello" const str2 = "World" const dp = Array.from({ length: str1.length }, () => Array.from({ length
阅读全文
摘要:const rtf = new Intl.RelativeTimeFormat( navigator.language ) const res = rtf.format(-2, 'day') console.log(res) // "2 days ago"
阅读全文
摘要:const calendarEvent = { title: 'abc submit', date: new Date(123), attendees: ["Steve", {name: 'Steve'}] } const copied = structuredClone(calendarEvent
阅读全文
摘要:In Javascript, if you are using Object to store key-valeu pairs while you will be adding and deleting keys frequently, then you should use Map instead
阅读全文
摘要:Promise.all: Problem: let's say we have two promises, P1, P2, P1 reject in 1s, and P2 reject in 3s. What will happen in catch block? It only able to c
阅读全文
摘要:Binding to Media Events import './assets/css/style.css'; import autumnMp4 from './assets/media/autumn.mp4'; const app = document.getElementById('app')
阅读全文
摘要:Array.from() is a great way to chunk up arrays because of the secondary argument being a map function. const hugeArray = Array.from({length: 76}, (_,
阅读全文
摘要:The Intl.Collator object enables language-sensitive string comparison. console.log(['Z', 'a', 'z', 'ä'].sort(new Intl.Collator('de').compare)); // exp
阅读全文
摘要:Since arrays are objects, we can destructure their indexes to easily grab the first and last itmes const bikes = ['bianchi', 'miele', 'miyata', 'benot
阅读全文
摘要:It is useful to enable '@typescript-eslint/unbound-method': 'error', because this kind of error is related to this keyword, sometime it is hard to not
阅读全文
摘要:The optimizing compiler optimizes for what it’s seen. If it sees something new, that’s problematic. Seleting properties has some strange implications
阅读全文
摘要:Code: benchmark.js const { performance } = require('perf_hooks'); // SETUP 🏁 let iterations = 1e7; const a = 1; const b = 2; const add = (x, y) => x
阅读全文
摘要:function countBehavior(state, event) { if (event.type "INC") { return { ...state, count: state.count + 1 } } } function createActor(behavior, initialS
阅读全文
摘要:Write a function that takes in an array of unique integers and returns an array of all permutations of those integers in no particular order. If the i
阅读全文
摘要:Source: https://javascriptpatterns.vercel.app/patterns/design-patterns/prototype-pattern If you use factory pattern to create object: const createDog
阅读全文