Today I Learnt

发表于更新于阅读时长 3 分钟

上一个太长了

无用的,反工程化的和有害的知识

2018.01.14 JS 中的函数都会被提升到最顶部, 如果出现重复则在语法分析阶段后面的就会覆盖前面的

2018.02.17 () => { x: 1 }返回数字 1, 此处 x 为 label

2018.03.16 在 JS 中,判断 B 是 A 的子类的方法是B.prototype instanceof A

2018.03.19 居然真有框架使用轮询的方式来 diff 数据

2019.03.26 JS 的 class 中的 getter 和 setter 定义在 prototype 上

2018.04.05 而这个框架的计算属性的依赖收集靠的是手动标注,使用方式如下

@computedFrom('firstName', 'lastName')

2018.05.02 JS 中 Array.prototype 还是个 Array

2018.05.13 Python 的 finally 一定会执行,因此如果在函数中 early return 的话 finally 会覆盖掉之前 return 的值,JS 里也是如此

2018.05.23 可以对 string 使用 Obejct.assign,不影响正常使用且可以访问子值,这是 string 被转化为了包装类的缘故

2018.05.28 Angular Router 只会 redirect 一次

2018.06.12 JavaScript 中可以使用 void 替代行首的;来避免二义性

2018.06.20 parserInt 的第二个参数是 falsy 值时将被忽略,将从第一个参数中猜测 radix

2018.06.28 TypeScript 中的类也是 structural type

2018.07.08 可以使用 num.toString().replace(/\B(?=(?=\d*.)(\d{3})+(?!\d))/g, ",")来给数字加上分隔符,当然最佳的做法是 Number.prototype.toLocaleString()

2018.07.09 IE7 和之前的 getElementById 会去匹配 name 属性,而 IE8 的 quirk mode 下也会发生同样的事情(这条真是特别没用)

2018.07.12 JavaScript 中新建一个内容为 1..n 的数组可以

new Array(n - 1).fill(0).map((_, i) => i + 1)
Array.from(new Array(n - 1), (_, i) => i + 1)
Array.from({ length: n - 1 }, (_, i) => i + 1)
// magic!
Array.apply(null, { length: n - 1 }).map((_, i) => i + 1)

因为 new Array(10)得到是 10 个 empty slot 的数组,而 empty slot 在迭代时会被忽略

2018.07.15 TypeScript 的 moduleResolution 默认值居然是 classic,这意味着它不会去 node_modules 里去找

2018.07.18 在 Array.from 出现之前,可以用 Array.prototype.slice.call 来把类数组对象(arguments, HTMLCollection)转化为数组

2018.07.28 javascript 的 new 操作符可以不用参数(即 new Foo)

2018.08.06 [].concat[1,2,3] 返回 undefined

2018.08.10 typeof 未声明的变量 会返回 undefined,即使在严格模式下也不会报错,但是访问了处于 temporal dead zone 的变量一定会报错

2018.08.15 在 ES6 中,可以使用如下方式的 obejct literal

const a = 'a'
const b = { [a]: 123 }

2018.08.16 isNaN(undefined)返回 true,而 Number.isNaN(undefined)返回 false

2018.08.17 js 中 array index 也会沿原型链上溯

2018.08.18 ES5 之前 undefined 是可写的

2018.09.03 当代 js 可以通过 varible.constructor.name 来判断变量类型,古代则只能使用 Object.prototype.toString.call 来判断

2018.09.13 arr.slice(0)比 arr.slice()更快,是最快的数组拷贝方式

2018.09.18 es6 里有

const a = 'test'
const b = { [a + 1]: () => {} }[a + 1]
b.name // "test1"

2018.09.23 string.prototype.toUpperCase 并不保证长度不变, 比如德语字母'ß'

2018.09.25 IE9 中 addEventListener 的第三个参数(useCapture)不是可选的

2018.10.01 new operator 作用的函数如果返回的是原始值,则会被忽略

2018.10.06 Typescript 可以把 2**7 转译成 Math.pow(2, 7)

2018.10.13 Reflect.getPrototypeOf 在当前标准中作用于原始值会报错,这是它和 Object.getPrototypeOf 的区别

2018.10.21 通过 childNodes 得到的 NodeList 是活的,其他时候不是.HTMLCollection 一定是活的

2018.11.02 JSON.stringify 接受三个参数,而 toJSON 则可以接受一个参数

2018.11.05 N+1 问题指的是数据库中出现 1 对 N 的关系时, 如果同时需要两边的信息, ORM 常常需要进行 N+1 次查询. 可以使用 eager load 解决

2018.11.14 最好使用 Object.prototype.hasOwnProperty.call 来判断一个 object 是否有特定的 key,因为由 Object.create(null)创建出来的 object 没有这个方法

2018.11.15 JS 不能 catch syntax error,除非是通过 eval 执行的代码

2018.11.16 JS 中函数的 length 是函数的直到第一个有默认值的参数或剩余参数前的参数的个数

2018.12.08 对于仅引用了类型的代码文件(即使是 class as type), typescript 在编译时不会打包进去

2018.12.15 可以用_ => foo 来代替 () => foo,也能通过 tslint

2018.12.16 fetch 会在拿到完整的 http 报文头之后立刻进入 fulfiled 状态

2018.12.18 超酷炫的 autocurry

const curry = (f, arr = []) => (...args) =>
  (a => (a.length === f.length ? f(...a) : curry(f, a)))([...arr, ...args])

2018.12.19 在 js 中可以通过把 Set/Map 转化成数组的方式按下标获取元素

2018.12.20 在 js 中如果想把可能包含 undefined 的变量转换成数字而不得到 NaN,可以使用 var >>> 0

© 2016 - 2023Austaras Devas