Skip to content

Commit 6a563ab

Browse files
trauma2utimruffles
authored andcommitted
add ko translation
1 parent f759e51 commit 6a563ab

File tree

6 files changed

+112
-66
lines changed

6 files changed

+112
-66
lines changed

doc/ko/array/general.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ JavaScript에서는 배열(Array)도 객체(Object)지만 객체 순회(Iterate)
2525

2626
`length` 프로퍼티의 *getter*는 단순히 Array 안에 있는 엘리먼트의 개수를 반환하고 *setter*는 배열을 할당한 수만큼 잘라 버린다.
2727

28-
var foo = [1, 2, 3, 4, 5, 6];
29-
foo.length = 3;
30-
foo; // [1, 2, 3]
28+
var arr = [1, 2, 3, 4, 5, 6];
29+
arr.length = 3;
30+
arr; // [1, 2, 3]
3131

32-
foo.length = 6;
33-
foo; // [1, 2, 3]
32+
arr.length = 6;
33+
arr.push(4);
34+
arr; // [1, 2, 3, undefined, undefined, undefined, 4]
3435

35-
현재 크기보다 더 작은 값을 할당하면 배열을 자르지만, 현재 크기보다 더 큰 값을 할당한다고 해서 배열을 늘리진 않는다.
36+
현재 크기보다 더 작은 값을 할당하면 배열을 자른다. 배열의 크기를 증가시키면 드문드문(sparse)한 배열을 생성한다.
3637

3738
### 결론
3839

doc/ko/core/eval.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
`eval` 함수는 JavaScript 문자열을 지역 스코프에서 실행한다.
44

5-
var foo = 1;
5+
var number = 1;
66
function test() {
7-
var foo = 2;
8-
eval('foo = 3');
9-
return foo;
7+
var number = 2;
8+
eval('number = 3');
9+
return number;
1010
}
1111
test(); // 3
12-
foo; // 1
12+
number; // 1
1313

1414
`eval`함수는 `eval`이라는 이름으로 **직접** 실행할 때에만 지역 스코프에서 실행된다. 그리고 `eval`이라는 이름에 걸맞게 악명또한 높다.
1515

16-
var foo = 1;
16+
var number = 1;
1717
function test() {
18-
var foo = 2;
19-
var bar = eval;
20-
bar('foo = 3');
21-
return foo;
18+
var number = 2;
19+
var copyOfEval = eval;
20+
copyOfEval('number = 3');
21+
return number;
2222
}
2323
test(); // 2
24-
foo; // 3
24+
number; // 3
2525

2626
어쨌든 `eval`은 사용하지 말아야 한다. eval을 사용하는 99.9%는 사실 eval 없이도 만들수있다.
2727

doc/ko/function/arguments.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,37 @@ JavaScript의 모든 함수 스코프에는 `arguments`라는 특별한 변수
2727
// 내곡동에 땅이라도 산다.
2828
}
2929

30-
또 다른 방법으로는 함수를 랩핑하지 않고, 풀어서 `call``apply`를 함께 사용하는 방법이 있다. (역주: 프로토타입에 있는 method를 호출하기 전에 Foo 객체 안에 있는 method로 한번더 필터링하는 효과가 있다. )
30+
또 다른 트릭은 `call``apply`를 함께 사용하여 메써드(`this`의 값과 인자들을 사용하는 함수)를
31+
단지 인자들만 사용하는 일반 함수로 바꾸는 것이다.
3132

32-
function Foo() {}
33+
function Person(first, last) {
34+
this.first = first;
35+
this.last = last;
36+
}
3337

34-
Foo.prototype.method = function(a, b, c) {
35-
console.log(this, a, b, c);
38+
Person.prototype.fullname = function(joiner, options) {
39+
options = options || { order: "western" };
40+
var first = options.order === "western" ? this.first : this.last;
41+
var last = options.order === "western" ? this.last : this.first;
42+
return first + (joiner || " ") + last;
3643
};
3744

38-
// "method"를 풀어 쓴(unbound) 버전
39-
// 이 Function의 인자: this, arg1, arg2...argN
40-
Foo.method = function() {
41-
42-
// 결과: Foo.prototype.method.call(this, arg1, arg2... argN)
43-
Function.call.apply(Foo.prototype.method, arguments);
45+
// "fullname" 메써드의 비결합(unbound) 버전을 생성한다.
46+
// 첫번째 인자로 'first'와 'last' 속성을 가지고 있는 어떤 객체도 사용 가능하다.
47+
// "fullname"의 인자 개수나 순서가 변경되더라도 이 랩퍼를 변경할 필요는 없을 것이다.
48+
Person.fullname = function() {
49+
// 결과: Person.prototype.fullname.call(this, joiner, ..., argN);
50+
return Function.call.apply(Person.prototype.fullname, arguments);
4451
};
4552

53+
var grace = new Person("Grace", "Hopper");
54+
55+
// 'Grace Hopper'
56+
grace.fullname();
57+
58+
// 'Turing, Alan'
59+
Person.fullname({ first: "Alan", last: "Turing" }, ", ", { order: "eastern" });
60+
4661
### 일반 파라미터와 arguments 객체의 인덱스
4762

4863
일반 파라미터와 `arguments` 객체의 프로퍼티는 모두 *getter**setter*를 가진다.

doc/ko/function/closures.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,21 @@ JavaScript에서는 스코프(Scope)를 어딘가에 할당해두거나 참조
7575
}
7676
})(i), 1000)
7777
}
78+
79+
즐겨 쓰이는 또 하나의 방법은 `setTimeout` 함수에 세번째 인자를 추가하는 방법이다.
80+
추가된 인자는 콜백 함수에 전달된다.
81+
82+
for(var i = 0; i < 10; i++) {
83+
setTimeout(function(e) {
84+
console.log(e);
85+
}, 1000, i);
86+
}
87+
88+
레거시 JS 환경(Internet Explorer 9 이하)은 이 방법을 지원하지 않는다.
89+
90+
`.bind`를 사용하는 방법도 있다. `.bind``this` 컨텍스트와 인자들을 함수에 결속(bind)시킨다.
91+
아래 코드는 위 코드와 동일하게 동작한다.
92+
93+
for(var i = 0; i < 10; i++) {
94+
setTimeout(console.log.bind(console, i), 1000);
95+
}

doc/ko/function/constructors.md

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,40 @@ JavaScript의 생성자는 다른 언어들과 다르게 `new` 키워드로 호
66

77
그리고 생성자에 명시적인 `return` 구문이 없으면 this가 가리키는 객체를 반환한다.
88

9-
function Foo() {
10-
this.bla = 1;
9+
function Person(name) {
10+
this.name = name;
1111
}
1212

13-
Foo.prototype.test = function() {
14-
console.log(this.bla);
13+
Person.prototype.logName = function() {
14+
console.log(this.name);
1515
};
1616

17-
var test = new Foo();
17+
var sean = new Person();
1818

19-
위 코드는 `new` 키워드가 실행되는 시점에 `Foo`를 생성자로 호출하고 `Foo.prototype`을 새 객체의 prototype에 할당한다.
19+
위 코드는 `Person`을 생성자로 호출하고 새로 생성된 객체의 `prototype``Person.prototype`으로 설정한다.
2020

2121
아래 코드와 같이 생성자에 명시적인 `return` 문이 있는 경우에는 반환하는 값이 객체인 경우에만 그 값을 반환한다.
2222

23-
function Bar() {
24-
return 2;
23+
function Car() {
24+
return 'ford';
2525
}
26-
new Bar(); // 새 객체를 만들어 반환
26+
new Car(); // 'ford'가 아닌 새로운 객체를 반환
2727

28-
function Test() {
29-
this.value = 2;
28+
function Person() {
29+
this.someValue = 2;
3030

3131
return {
32-
foo: 1
32+
name: 'Charles'
3333
};
3434
}
35-
new Test(); // 명시한 객체를 반환
35+
new Person(); // someValue가 포함되지 않은 ({name:'Charles'}) 객체 반환
3636

3737
new 키워드가 없으면 그 함수는 객체를 반환하지 않는다.
3838

39-
function Foo() {
40-
this.bla = 1; // 전역객체에 할당된다.
39+
function Pirate() {
40+
this.hasEyePatch = true; // 전역 객체를 준비!
4141
}
42-
Foo(); // undefined
42+
var somePirate = Pirate(); // somePirate = undefined
4343

4444
위 예제는 그때그때 다르게 동작한다. 그리고 [`this`](#function.this) 객체의 동작 원리에 따라서 Foo 함수안의 `this`의 값은 *Global 객체*를 가리키게된다.
4545
(역주: 결국 new 키워드를 빼고, 코드를 작성할 경우 원치 않은 this 참조 오류가 발생할 수 있다.)
@@ -48,24 +48,24 @@ new 키워드가 없으면 그 함수는 객체를 반환하지 않는다.
4848

4949
생성자가 객체를 반환하면 `new` 키워드를 생략할 수 있다.
5050

51-
function Bar() {
52-
var value = 1;
51+
function Robot() {
52+
var color = 'gray';
5353
return {
54-
method: function() {
55-
return value;
54+
getColor: function() {
55+
return color;
5656
}
5757
}
5858
}
59-
Bar.prototype = {
60-
foo: function() {}
59+
Robot.prototype = {
60+
someFunction: function() {}
6161
};
6262

63-
new Bar();
64-
Bar();
63+
new Robot();
64+
Robot();
6565

66-
new 키워드의 유무과 관계없이 `Bar` 생성자의 동작은 동일한다. 즉 [클로저](#function.closures)가 할당된 method 프로퍼티가 있는 새로운 객체를 만들어 반환한다.
66+
new 키워드의 유무과 관계없이 `Robot` 생성자의 동작은 동일하다. 즉 [클로저](#function.closures)가 할당된 method 프로퍼티가 있는 새로운 객체를 만들어 반환한다.
6767

68-
`new Bar()` 호출되는 생성자는 반환되는 객체의 prototype 프로퍼티에 아무런 영향을 주지 않는다. 객체를 반환하지 않는 생성자로 만들어지는 경우에만 객체의 prototype이 생성자의 것으로 할당된다.
68+
`new Robot()`으로 호출되는 생성자는 반환되는 객체의 prototype 프로퍼티에 아무런 영향을 주지 않는다. 객체를 반환하지 않는 생성자로 만들어지는 경우에만 객체의 prototype이 생성자의 것으로 할당된다.
6969

7070
그러니까 이 예제에서 `new` 키워드의 유무는 아무런 차이가 없다.
7171
(역주: 생성자에 객체를 만들어 명시적으로 반환하면 new 키워드에 관계없이 잘 동작하는 생성자를 만들수있다. 즉, new 키워드가 빠졌을때 발생하는 this 참조 오류를 방어해준다.)
@@ -76,19 +76,21 @@ new 키워드의 유무과 관계없이 `Bar` 생성자의 동작은 동일한
7676

7777
객체를 만들고 반환해주는 팩토리를 사용하여 `new` 키워드 문제를 회피할 수 있다.
7878

79-
function Foo() {
80-
var obj = {};
81-
obj.value = 'blub';
79+
function CarFactory() {
80+
var car = {};
81+
car.owner = 'nobody';
8282

83-
var private = 2;
84-
obj.someMethod = function(value) {
85-
this.value = value;
83+
var milesPerGallon = 2;
84+
85+
car.setOwner = function(newOwner) {
86+
this.owner = newOwner;
8687
}
8788

88-
obj.getPrivate = function() {
89-
return private;
89+
car.getMPG = function() {
90+
return milesPerGallon;
9091
}
91-
return obj;
92+
93+
return car;
9294
}
9395

9496
`new` 키워드가 없어도 잘 동작하고 [private 변수](#function.closures)를 사용하기도 쉽다. 그렇지만, 단점도 있다.

doc/ko/function/this.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,26 @@ Global Scope에서도 this가 사용될 수 있고 이때에는 *Global* 객체
5858
`test`에서 `Foo`에 접근하려면 method에 Local 변수를 하나 만들고 `Foo`를 가리키게 하여야 한다.
5959

6060
Foo.method = function() {
61-
var that = this;
61+
var self = this;
6262
function test() {
63-
// 여기에서 this 대신에 that을 사용하여 Foo에 접근한다.
63+
// 여기에서 this 대신에 self를 사용하여 Foo에 접근한다
6464
}
6565
test();
6666
}
6767

68-
`that`은 this에 접근하기 위해 만든 변수다. [closures](#function.closures)와 함께 `this`의 값을 넘기는 데 사용할 수 있다.
68+
`self`는 통상적인 변수 이름이지만, 바깥쪽의 `this`를 참조하기 위해 일반적으로 사용된다.
69+
또한 [클로저](#function.closures)와 결합하여 `this`의 값을 주고 받는 용도로 사용할 수도 있다.
6970

70-
### Method할당 하기
71+
ECMAScript 5부터는 익명 함수와 결합된 `bind` 메써드를 사용하여 같은 결과를 얻을 수 있다.
72+
73+
Foo.method = function() {
74+
var test = function() {
75+
// this는 이제 Foo를 참조한다
76+
}.bind(this);
77+
test();
78+
}
79+
80+
### Method 할당하기
7181

7282
JavaScript의 또다른 함정은 바로 함수의 별칭을 만들수 없다는 점이다. 별칭을 만들기 위해 메소드를 변수에 넣으면 자바스크립트는 별칭을 만들지 않고 바로 *할당*해 버린다.
7383

0 commit comments

Comments
 (0)