Skip to content

Commit dbbec5e

Browse files
committed
First Commit
0 parents  commit dbbec5e

File tree

409 files changed

+498620
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

409 files changed

+498620
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

ch1/.DS_Store

6 KB
Binary file not shown.

ch1/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*** a.txt ***

ch1/anonymous.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 익명 함수를 사용해 함수를 정의합니다.
2+
const f1 = function (s) { console.log(s) }
3+
const f2 = (s) => { console.log(s) }
4+
// 익명 함수는 일반적인 함수처럼 사용합니다.
5+
f1('foo')
6+
f2('bar')

ch1/anonymous2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 소문자를 대문자로 변환하는 예
2+
const s = 'Keep On Asking, and It Will Be Given You.'
3+
const r = s.replace(/[a-z]+/g, function (m) {
4+
return m.toUpperCase()
5+
})
6+
console.log(r)
7+
8+
// 배열의 숫자를 정렬하는 예
9+
const ar = [100, 1, 20, 43, 30, 11, 4]
10+
ar.sort((a, b) => { return b - a })
11+
console.log(ar)

ch1/arrow-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// 화살표 함수의 예
2+
const x3 = (n) => n * 3

ch1/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*** b.txt ***

ch1/babeltest/.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "presets": ["es2015"] }

ch1/babeltest/bmi.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// BMI 클래스를 정의합니다.
2+
class BMI {
3+
constructor (height, weight) {
4+
this.height = height
5+
this.weight = weight
6+
this.bmi = this.calc()
7+
}
8+
calc () {
9+
const heightM = this.height / 100
10+
return this.weight / (heightM ** 2)
11+
}
12+
print () {
13+
let res = '표준'
14+
if (this.bmi >= 25) res = '비만'
15+
else if (this.bmi >= 18.5) res = '표준'
16+
else res = '저체중'
17+
console.log('BMI =', this.bmi, res)
18+
}
19+
}
20+
// 테스트
21+
const bmi = new BMI(160, 60)
22+
bmi.print()

ch1/babeltest/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html><meta charset="utf-8"><body>
2+
<script src="bmi.out.js"></script>
3+
</body></html>
4+

ch1/babeltest/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "babeltest",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"build": "babel bmi.js -o bmi.out.js",
7+
"watch": "babel bmi.js -w -o bmi.out.js",
8+
"start": "node bmi.out.js"
9+
},
10+
"devDependencies": {
11+
"babel-cli": "^6.23.0",
12+
"babel-preset-es2015": "^6.22.0"
13+
}
14+
}

ch1/babeltest/src/a.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setTimeout(() => {
2+
console.log('foo')
3+
}, 100)

ch1/babeltest/src/b.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Foo 클래스를 정의합니다.
2+
class Foo {
3+
constructor () {
4+
this.value = 100
5+
}
6+
bar() {
7+
console.log('Foo.bar')
8+
console.log(this.value)
9+
}
10+
}
11+
// Foo를 사용합니다.
12+
const f = new Foo()
13+
f.bar()

ch1/c.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*** c.txt ***

ch1/calculator_main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// 모듈을 읽어 들입니다.
2+
const calculator = require('./calculator_module.js')
3+
// 모듈의 함수를 사용합니다.
4+
console.log('3+5=', calculator.add(3, 5))
5+
console.log('4*8=', calculator.mul(4, 8))

ch1/calculator_module.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 덧셈과 곱셈하는 함수
2+
function add (a, b) {
3+
return a + b
4+
}
5+
function mul (a, b) {
6+
return a * b
7+
}
8+
// 외부에 공개합니다.
9+
module.exports = {
10+
'add': add,
11+
'mul': mul
12+
}

ch1/dice-server.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// http 모듈을 읽어 들입니다.
2+
const http = require('http')
3+
const ctype = { 'Content-Type': 'text/html;charset=utf-8' }
4+
5+
// 웹 서버를 실행합니다. --- (※1)
6+
const svr = http.createServer(handler) // 서버를 생성합니다.
7+
svr.listen(8081) // 8081번 포트를 사용합니다.
8+
9+
// 서버에 접근이 있을 때의 처리 --- (※2)
10+
function handler (req, res) {
11+
// URL을 구분합니다.
12+
const url = req.url
13+
// 최상위 페이지일 때
14+
if (url === '/' || url === '/index.html') {
15+
showIndexPage(req, res)
16+
return
17+
}
18+
// 주사위 페이지일 때
19+
if (url.substr(0, 6) === '/dice/') {
20+
showDicePage(req, res)
21+
return
22+
}
23+
// 그 밖의 경우
24+
res.writeHead(404, ctype)
25+
res.end('404 not found')
26+
}
27+
28+
// 인덱스 페이지에 접근했을 경우 --- (※3)
29+
function showIndexPage (req, res) {
30+
// HTTP 헤더를 출력합니다.
31+
res.writeHead(200, ctype)
32+
// 응답 본문을 출력합니다.
33+
const html = '<h1>주사위 페이지 안내</h1>\n' +
34+
'<p><a href="/dice/6">6면체 주사위</a></p>' +
35+
'<p><a href="/dice/12">12면체 주사위</a></p>'
36+
res.end(html)
37+
}
38+
39+
// 주사위 페이지에 접근했을 때 --- (※4)
40+
function showDicePage (req, res) {
41+
// HTTP 헤더를 출력합니다.
42+
res.writeHead(200, ctype)
43+
// 몇 면체 주사위가 필요한지 확인합니다.
44+
const a = req.url.split('/')
45+
const num = parseInt(a[2])
46+
// 임의 숫자를 생성합니다.
47+
const rnd = Math.floor(Math.random() * num) + 1
48+
// 응답 본문을 출력합니다.
49+
res.end('<p style="font-size:72px;">' + rnd + '</p>')
50+
}

ch1/es2015io/.DS_Store

6 KB
Binary file not shown.

ch1/es2015io/.babelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "presets": ["es2015"] }
2+

ch1/es2015io/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "es2015io",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"babel-cli": "^6.23.0",
14+
"babel-preset-es2015": "^6.22.0"
15+
}
16+
}

ch1/es2015io/src/calctest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// 외부에 공개할 함수를 정의합니다.
2+
export function add (a, b) {
3+
return a + b
4+
}
5+
export function mul (a, b) {
6+
return a * b
7+
}

ch1/es2015io/src/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "calctest.js" 모듈을 읽어 들입니다.
2+
import {add, mul} from './calctest.js'
3+
// 읽어 들인 함수를 사용합니다.
4+
console.log(add(2, 3))
5+
console.log(mul(6, 8))

ch1/es2015io/src/main2.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// 모듈을 읽어 들입니다.
2+
import * as ct from './calctest.js'
3+
// 모듈의 함수를 사용합니다.
4+
console.log(ct.add(2, 3))
5+
console.log(ct.mul(6, 8))

ch1/es2015io/src/main3.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// 모듈을 읽어 들입니다.
2+
import {add as addF, mul as mulF} from './calctest.js'
3+
// 모듈의 함수를 사용합니다.
4+
console.log(addF(2, 3))
5+
console.log(mulF(6, 8))

ch1/es2015io2/.babelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "presets": ["es2015"] }
2+

ch1/es2015io2/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "es2015io2",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"babel-cli": "^6.24.1",
14+
"babel-preset-es2015": "^6.24.1"
15+
}
16+
}

ch1/es2015io2/src/gobsem.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 함수를 정의합니다.
2+
function gobsem(a, b) {
3+
return a * b;
4+
}
5+
// gobsem을 기본적으로 공개합니다.
6+
export default gobsem;

ch1/es2015io2/src/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// gobsem 모듈을 읽어 들입니다.
2+
import gobsem from './gobsem';
3+
// gobsem 함수를 사용합니다.
4+
const v = gobsem(2, 3);
5+
console.log(v);

ch1/fib.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 피보나치 수를 계산하는 제너레이터 함수를 정의합니다.
2+
function * genFibonacci () {
3+
let a = 0
4+
let b = 1
5+
while (true) {
6+
[a, b] = [b, a + b]
7+
yield a
8+
}
9+
}
10+
11+
// 제너레이터 객체를 추출합니다.
12+
const fib = genFibonacci()
13+
for (const num of fib) {
14+
// 무한 반복하게 되므로 50을 넘으면 탈출하게 합니다.
15+
if (num > 50) break
16+
console.log(num)
17+
}

ch1/generator-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 제너레이터 함수를 정의합니다. --- (※1)
2+
function * counter () {
3+
yield 1
4+
yield 2
5+
yield 3
6+
}
7+
// 제너레이터 객체를 생성합니다. --- (※2)
8+
const g = counter()
9+
// next() 메서드를 호출합니다. --- (※3)
10+
console.log(g.next())
11+
console.log(g.next())
12+
console.log(g.next())
13+
console.log(g.next())

ch1/hello-server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// http 모듈을 읽어 들입니다.
2+
const http = require('http')
3+
4+
// 웹 서버를 실행합니다. --- (※1)
5+
const svr = http.createServer(handler) // 서버를 생성합니다.
6+
svr.listen(8081) // 8081번 포트를 사용합니다.
7+
8+
// 서버에 접근이 있을 때의 처리 --- (※2)
9+
function handler (req, res) {
10+
console.log('url:', req.url)
11+
console.log('method:', req.method)
12+
// HTTP 헤더를 출력합니다.
13+
res.writeHead(200, {'Content-Type': 'text/html'})
14+
// 응답 본문을 출력합니다.
15+
res.end('<h1>Hello, World!</h1>\n')
16+
}

ch1/project_a/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const colors = require('colors');
2+
3+
const s1 = "A time to throw stones away";
4+
const s2 = "and a time to gather stones together";
5+
const s3 = "A time to search and a time to give up as lost";
6+
const s4 = "A time to keep and a time to throw away";
7+
8+
console.log(s1.underline.red);
9+
console.log(s2.inverse.blue);
10+
console.log(s3.rainbow);
11+
console.log(s4.inverse.red);
12+

ch1/project_a/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "project_a",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"start": "node index.js",
6+
"check": "node -v"
7+
},
8+
"dependencies": {
9+
"colors": "^1.1.2"
10+
}
11+
}

ch1/readfile-async.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require('fs')
2+
// 프로미스로 비동기적으로 파일을 읽어 들이는 함수를 정의합니다.
3+
function readFileEx (fname) {
4+
return new Promise((resolve, reject) => {
5+
fs.readFile(fname, 'utf-8', (err, data) => {
6+
resolve(data)
7+
})
8+
})
9+
}
10+
// 모든 파일을 읽어 들이는 async 함수를 정의합니다.
11+
async function readAll () {
12+
const a = await readFileEx('a.txt')
13+
console.log(a)
14+
const b = await readFileEx('b.txt')
15+
console.log(b)
16+
const c = await readFileEx('c.txt')
17+
console.log(c)
18+
}
19+
readAll()

ch1/readfile-cb.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const fs = require('fs')
2+
// 파일을 읽어 들입니다.
3+
fs.readFile('test.txt', 'utf-8', function (err, data) {
4+
// 읽어 들이기를 완료했을 때의 처리
5+
console.log(data)
6+
})

ch1/readfile-cb2.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fs = require('fs')
2+
3+
fs.readFile('test.txt', 'utf-8', (err, data) => {
4+
console.log(data)
5+
})

ch1/readfile-cb3.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require('fs')
2+
// (1) a.txt를 읽어 들입니다.
3+
fs.readFile('a.txt', 'utf-8', function (err, data) {
4+
console.log('a.txt를 읽어 들였습니다.', data)
5+
// (2) b.txt를 읽어 들입니다.
6+
fs.readFile('b.txt', 'utf-8', function (err, data) {
7+
console.log('b.txt를 읽어 들였습니다.', data)
8+
// (3) c.txt를 읽어 들입니다.
9+
fs.readFile('c.txt', 'utf-8', function (err, data) {
10+
console.log('c.txt를 읽어 들였습니다.', data)
11+
})
12+
})
13+
})

0 commit comments

Comments
 (0)