Skip to content

Javascript Note

  • About NodeJS see Node Note
  • @2021-12-24: Renamed ES6 Note to JS Note since ES next, es 2021, es2022

Overloading a built-in Class#

Class inheritance#

  • @2021-10-04
  • class B extends A needs to call super() and the return value(constructed class instance) of A.constructor() is stored in B.this.

Online sandbox#

  • @2021-10-08
  • Stackblitz vs CodeSandbox
  • Stackblitz is much faster but the package can only be imported from its own NPM system.
  • CodeSandbox can add preview packages

Object.assign()#

  • @2021-10-21
  • will change first var.

Array Trap#

  • @2021-10-21
  • Trap: arr.entries() and arr.forEach() takes different (ind,val) order: [ind,val] in arr.entries() and arr.forEach((val,ind)=>null)

BigInt#

  • @2021-10-21
  • Is a primitive JS type
  • Has literal expression (BigInt(5) === 5n;)
  • Division will automatic floor.

String Template#

  • @2021-12-16
  • [ ] TODO : ruanyifeng
1
2
3
4
5
6
7
const FooterWrap = styled('div')({
color: 'red',
});

// const FooterWrap = styled('div')`
// color: red;
//`;

Typing and JSDocs#

Tagged template#

Proxy#

Throw Error#

  • @2022-01-03
  • throw Error('msg') and throw new Error('msg') does the same thing. Because Error('msg') and new Error('msg') are the same thing.
  • Full answer on Stack Overflow

Import and Export#

Import raw string#

Why folder-level index.js#

  • @2022-01-11
  • It's just for importing and exporting easier. The name is confusing, we can make it asset/asset.ts too. The problem is not the index.ts es. Rather a bad pattern on using them. We shouldn’t write any code in index.ts, instead only export and import, and maybe some typing definition.
  • origin

Async iterator to array#

1
2
3
4
5
async function toArray(asyncIterator) {
  const arr = [];
  for await (const i of asyncIterator) arr.push(i);
  return arr;
}