package.json
npm ๋ชจ๋์ ํ์ฉํ๊ธฐ ์ํด ํด๋น ๋ชจ๋์ ๋ํ ์ ๋ณด๋ฅผ ๋ด์ ํ์ผ
์ฆ, ํ๋ก์ ํธ ์ ๋ฐ์ ๊ดํ ์ ๋ณด๊ฐ ๋ด๊ฒจ์๋ค.
npm : node.js ์ํ๊ณ์ ํจํค์ง ๋งค๋์
{
"name": "modern-javascript-koans",
"version": "1.0.0",
"description": "",
"main": "index.js",
// ํ๋ก์ ํธ์ ๊ดํ ์ ๋ณด
"scripts": {
"test": "mocha modern-js-koans/*.js --sort",
"report": "mocha modern-js-koans/*.js --sort --reporter @mochajs/json-file-reporter",
"submit": "codestates-submission"
},
"keywords": [],
"author": "codesatates",
//์คํฌ๋ฆฝํธ๋ CLI์์ ์ฌ์ฉ๊ฐ๋ฅํ ๋ช
๋ น๋ค
"dependencies": {
"@codestates-cc/submission-npm": "^1.1.1"
},
"devDependencies": {
"@mochajs/json-file-reporter": "^1.2.1",
"chai": "^4.2.0",
"mocha": "^8.2.0",
"sinon": "^9.0.3"
}
// ๊ฐ๋ฐ๊ณผ ๊ด๋ จ๋ dependency๋ค
}
์ฌ์ฉ์ ์ ํ ๋ชจ๋์ ๋ค์ด๋ก๋ => $ npm install
๋ชจ๋์ ์ด์ฉํ์ฌ test ์ฌ์ฉ๋ฒ => $ npm run test
๋ชจ๋์ ์ด์ฉํ์ฌ ์ ์ถ => $ npm run submit
ํ์ดํ ํจ์
ํจ์ํํ์
const add = function (x, y) {
return x + y
}
ํ์ดํ ํจ์๋ก
const add = (x, y) => {
return x + y;
}
ํ์ดํ ํจ์ ๊ท์น
ํจ์ ๋ณธ๋ฌธ(body)์ ๋ฆฌํด๋ฌธ ํ์ค ๋ง ์์ ๊ฒฝ์ฐ return์ ์๋ตํ ์ ์๋ค, ์ฃผ์ ํด์ผ ๋ ์ ์ { } (์ค๊ดํธ)๋ ์ฌ์ฉํ๋ฉด ์๋๋ค.
๋ง์ด ์ฐ๋ ํ์ดํ ํจ์์ ํด๋ก์ ธํจ์ ์์
ํจ์ ํํ์
const adder = function(x) {
return function(y) {
return x + y
}
}
ํ์ดํ ํจ์๋ก
const adder = x => {
return y => {
return x + y;
}
}
CommonJS
๋ชจ๋ ๋ชจ๋์ ๋
๋ฆฝ์ ์ธ ๊ณต๊ฐ์ ๊ฐ์ ธ์ผ ํ๋ค.
commonJS๋ ํ๋์ ํจ์๋ผ๊ณ ์๊ฐ.
module.exports๋ฅผ ์ฌ์ฉํด JSํ์ผ์ ๋ชจ๋ํ ์ํจ๋ค.
exports๋ module.exports๋ฅผ ์ถ์ฝํ ์ํจ๊ฒ ์ด๋ค.
<hello.js>
let x = 10;
module.exports.x = 20;
requier๋ฅผ exports ๊ฐ์ฒด๋ฅผ ๋ถ๋ฌ์จ๋ค.
const get = require("./hello.js")
// get์๋ 20์ด ๋ด๊ธด๋ค.