- Deadline: 10/12 中午 12:00
- 把檔案寄至 [email protected],標題寫 [作業二/ 電機X XXX] (如果你已經會用 github 也可以寄 repo 連結過來)
這個 function 需通過以下測試案例:
getType(1) // 'number'
getType(NaN) // 'NaN'
getType('1') // 'string'
getType(function() {}) // 'function'
getType({}) // 'object'
getType([]) // 'array'
getType(null) // 'null'
getType(undefined) // 'undefined'
- 參照投影片內關於
typeof
的陷阱
count
: 必須放在 closure 內作為 private variable,不能給別人直接修改,初始化為 0getCount
: 作為取用到 count value 的唯一 public method,回傳count
當下的值increase
: 把 closure 內的count
加一decrease
: 把 closure 內的count
減一
- 參考課程上 closure 的例子,把 public method return 出來,其他 private 的放在 closure 內
3. 寫一個 curring 化 (這不是個容易講解的詞彙,請往下看) 的 sum
function
一個一般的 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) 這樣的呼叫方式。
- 需要用到 High-order function (接收 function 當參數或是以 function 當回傳值的 function)
- Currying 的中文維基百科更好懂一些
- 需要安裝
eslint
,eslint-config-airbnb-base
,eslint-plugin-import@1
為devDependencies
- 寫
.eslintrc.js
讓 eslint 使用airbnb-base
這套規則去做 lint - 上述的作業需要用 eslint 跑過,沒有出現 error
- 如果有不喜歡的 style 可以在
.eslintrc.js
用 rules 覆寫
- 下載 v4.x (LTS) or v6.x (stable & latest):https://nodejs.org/en/
- apt-get 可能會裝到遠低於目前要求的 node v0.10.x 與 npm 1.x (三四年前的),請確定 node >=4, npm >=2
- 建議 Linux 和 OSX 的人,安裝 n 或是 nvm,方便隨時切換 node 版本
- 使用 Windows 的人建議安裝 Cygwin,可以有比較接近 Linux 的開發體驗
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/');
請安裝 command line 的工具,避免使用 GUI 工具
- 申請 github 帳號
- 在自己電腦跟 github 產生與設定 ssh key
- Github hello