随笔分类 - Javascript
摘要:Problem to Solve Share functionality between classes without using inheritance. Solution Create a class containing methods that can be used by other c
阅读全文
摘要:class ApplicationError extends Error { get name() { return this.constructor.name; } } class DatabaseError extends ApplicationError {} class UserFacing
阅读全文
摘要:class Product { name; price; constructor(name, price) { this.name = name; try { this.price = price * 1.30; } catch(e) { throw new Error("Product canno
阅读全文
摘要:Docs: https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit/ /* This is a recursive function without PTC */ function fatorial(n) { if
阅读全文
摘要:You don't really need to apply a really complex regex for one validation, instead you can combine multi regex together to fulfilll the task. const reg
阅读全文
摘要:Any time when you have non-primitive type, it's going to be removed from memory anytime if it is no longer needed. class Test { constructor(name) { th
阅读全文
摘要:function currency(strings, ...values) { return strings.reduce((result, string, i) => { let value = values[i - 1]; if (typeof value "number") { value =
阅读全文
摘要:The new cause data property that you can add to a thrown Error can be used to retain access to the original error caught in a promise rejection. const
阅读全文
摘要:If we want to be able to modify the state of a promise from outside the constructor, we can use the Promise.withResolvers method to get access to the
阅读全文
摘要:The new Array.with method gives you an immutable syntax for changing values of an array at a specified index. Sometimes .map will be more efficient. S
阅读全文
摘要:You no longer need to write generator functions to create basic lazy iterator functions. Now, ECMAScript supports iterator helpers that you can call o
阅读全文
摘要:The "Set Methods" proposal introduces 7 new methods to JavaScript/ECMAScript: union(setB): Returns a new Set containing all elements from the original
阅读全文
摘要:Finding elements starting from the end of an array has gotten a lot easier with the introduction of the at, findLast, and findLastIndex methods! With
阅读全文
摘要:Array Grouping is the new feature of JavaScript/ECMAScript, which splits an array (or, generally, an iterable), into smaller sub-arrays. Grouping is d
阅读全文
摘要:Intro to Array.prototype.with(index, value) const ages = [10, 15, 20, 25]; const newAges = ages.with(1, 16); console.log(newAges); // [10, 16, 20, 25]
阅读全文
摘要:function thankYouTag(arrayOfStrings,firstExpression, secondExpression, ...) { console.log( arrayOfStrings, // ["This is the first one ", " This is ano
阅读全文
摘要:Symbol.for("key") Checks if a symbol with the key "key" already exists in the globals symbol registry. If yes, it returns the existing symbol, otherwi
阅读全文
摘要:Difference betwen require and import require can be called conditionally, while the import statement cannot import statements are hoisted, but require
阅读全文
摘要:Generator can run with for .. of and ..., which will only emit yield values For example: function* count() { yield 1; yield 2; return 3; } for (const
阅读全文
摘要:Create a new User instance would create a new login function in memory each time? class User { constructor(username) { this.username = username; } log
阅读全文