Tianhe Gao

把代码像小说一样进行推荐

为什么说读代码像读小说?| 王建硕

anime/anime.js at master · juliangarnier/anime

 1    var is = {
 2      arr: function (a) {
 3        return Array.isArray(a)
 4      },
 5      obj: function (a) {
 6        return stringContains(Object.prototype.toString.call(a), 'Object')
 7      },
 8      pth: function (a) {
 9        return is.obj(a) && a.hasOwnProperty('totalLength')
10      },
11      svg: function (a) {
12        return a instanceof SVGElement
13      },
14      inp: function (a) {
15        return a instanceof HTMLInputElement
16      },
17      dom: function (a) {
18        return a.nodeType || is.svg(a)
19      },
20      str: function (a) {
21        return typeof a === 'string'
22      },
23      fnc: function (a) {
24        return typeof a === 'function'
25      },
26      und: function (a) {
27        return typeof a === 'undefined'
28      },
29      nil: function (a) {
30        return is.und(a) || a === null
31      },
32      hex: function (a) {
33        return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(a)
34      },
35      rgb: function (a) {
36        return /^rgb/.test(a)
37      },
38      hsl: function (a) {
39        return /^hsl/.test(a)
40      },
41      col: function (a) {
42        return is.hex(a) || is.rgb(a) || is.hsl(a)
43      },
44      key: function (a) {
45        return (
46          !defaultInstanceSettings.hasOwnProperty(a) &&
47          !defaultTweenSettings.hasOwnProperty(a) &&
48          a !== 'targets' &&
49          a !== 'keyframes'
50        )
51      },
52    }

在这里 === 表达了更严格的相等,其实使用 == 效果也是一样的。计算机执行不会报错,但是为什么还是要使用 === 呢?我认为作者是想让人们阅读代码时有一种更舒适的感受。曾经听到过一句话,好的程序员不仅能写出计算机理解的代码,还能写出人能理解的代码。


No notes link to this note

Welcome to tell me your thoughts via "email"
UP