TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • Brevity is the soul of wit
  • One-letter variables
  • Use abbreviations
  • Soar high. Be abstract.
  • Smart synonyms
  • Reuse names
  • Underscores for fun
  • Show your love
  • Overlap outer variables
  • Side-effects everywhere!
  • Powerful functions!

Was this helpful?

  1. JavaScript

Ninja code

学而不思则罔,思而不学则殆。過去的工程師使用這些技巧,讓別人看不懂程式碼。

Brevity is the soul of wit

極簡的極致,相看兩不厭,只有一行碼。

// taken from a well-known javascript library
i = i ? i < 0 ? Math.max(0, len + i) : i : 0;

One-letter variables

一個字母的變數,沒人知道代表的意思。

Use abbreviations

只有通靈王能知道縮寫代表的意思,盡情使用吧!

  • list → lst.

  • userAgent → ua.

  • browser → brsr.

  • …etc

Soar high. Be abstract.

  • 最適合變數的名稱 data value 乍看之下很正常,實際上沒有任何意義。

  • 用資料類型命名,str num

  • 找不到更多變數,後面加數字 item2 data1

Smart synonyms

對於同樣的事情用不同前綴,不同的兩件事情用相同的前綴。

  • displayMessage vs showName (顯示在螢幕上)

  • printPage (印出來) vs printText (顯示在螢幕上)

Reuse names

重複使用已存在的變數,更進階的用法式在函式中間重新使用。

function ninjaFunction(elem) {
  // 20 lines of code working with elem

  elem = clone(elem);

  // 20 more lines, now working with the clone of the elem!
}

Underscores for fun

在變數前使用 _ __ 而且沒人知道他們代表的意思。

  1. 降低可讀性

  2. 花很長時間弄清楚下畫線的意思

Show your love

寫的一堆沒用的形容詞,superElement megaFrame niceItem 。

Overlap outer variables

let user = authenticateUser();

function render() {
  let user = anotherValue();
  ...
  ...many lines...
  ...
  ... // <-- a programmer wants to work with user here and...
  ...
}

Side-effects everywhere!

isReady() checkPermission() 假設只會執行內部運算不會影響函式外的變數,結果使用時改變了外面的變數或返回的值不是布林值,你同事的表情一臉矇逼爽!

Powerful functions!

函數不只執行一個動作時,你的同事用不著這個函式,他就不會來煩你了。

PreviousCommentsNextAutomated testing with mocha

Last updated 5 years ago

Was this helpful?