Skip to content

Instantly share code, notes, and snippets.

@stegben
Forked from kpman/week-2.md
Last active March 7, 2017 13:40
Show Gist options
  • Save stegben/4f538320225be0e339dd641a420600aa to your computer and use it in GitHub Desktop.
Save stegben/4f538320225be0e339dd641a420600aa to your computer and use it in GitHub Desktop.
Week 2 - After class

Week 2

Practice

1. 寫一個判斷並回傳型別的 function getType

Requirement

這個 function 需通過以下測試案例:

getType(1) // 'number'
getType(NaN) // 'NaN'
getType('1') // 'string'
getType(function() {}) // 'function'
getType({}) // 'object'
getType([]) // 'array'
getType(null) // 'null'
getType(undefined) // 'undefined'

Hint

  • 參照投影片內關於 typeof 的陷阱

2. 寫一個 counter

Requirement

  • count: 必須放在 closure 內作為 private variable,不能給別人直接修改,初始化為 0
  • getCount: 作為取用到 count value 的唯一 public method,回傳 count 當下的值
  • increase: 把 closure 內的 count 加一
  • decrease: 把 closure 內的 count 減一

Hint

  • 參考課程上 closure 的例子,把 public method return 出來,其他 private 的放在 closure 內

3. 寫一個 curring 化 (這不是個容易講解的詞彙,請往下看) 的 sum function

Requirement

一個一般的 sum function 長這個樣子:

function sum(a, b, c) {
  return a + b + c;
}

sum(1, 2, 3) // 6
sum(100, 25, 10) // 135

請寫一個 curringSum function 他是一個一個給參數,這樣使用:

curringSum(1)(2)(3); // 6
var add125 = curringSum(100)(25);
add125(10); // 135

備註:這邊不算是標準的 currying,不然應該也要支援 curringSum(1, 2)(3) 這樣的呼叫方式。

Hint

  • 需要用到 High-order function (接收 function 當參數或是以 function 當回傳值的 function)
  • Currying 的中文維基百科更好懂一些

4. 實際操作 ESLint

Requirement

  • 需要安裝 eslint, eslint-config-airbnb-base, eslint-plugin-import@1devDependencies
  • .eslintrc.js 讓 eslint 使用 airbnb-base 這套規則去做 lint
  • 上述的作業需要用 eslint 跑過,沒有出現 error

Hint

  • 如果有不喜歡的 style 可以在 .eslintrc.js 用 rules 覆寫

Next Week Pre-reading

Node.js

node & npm

CommonJS Module
安裝 node (附帶 npm)
Note:
  • apt-get 可能會裝到遠低於目前要求的 node v0.10.x 與 npm 1.x (三四年前的),請確定 node >=4, npm >=2
  • 建議 Linux 和 OSX 的人,安裝 n 或是 nvm,方便隨時切換 node 版本
  • 使用 Windows 的人建議安裝 Cygwin,可以有比較接近 Linux 的開發體驗
確定能成功執行 Hello World
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

http

git 版本控管工具

上手
安裝 git

請安裝 command line 的工具,避免使用 GUI 工具

熟悉 github
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment