Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

New in JavaScript 1.8

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

자바스크립트 1.8은 (Firefox 3에 포함될 예정인) Gecko 1.9의 일부분으로 계획되어 있습니다. 자바스크립트 1.8은 자바스크립트 1.7보다는 변경된 부분이 적습니다. 그러나, ECMAScript 4/JavaScript 2로 진행되는 과정의 몇몇 변경점이 있습니다. 이번 자바스크립트 1.8 릴리즈에는 자바스크립트 1.6자바스크립트 1.7에 있는 모든 부분이 포함될 것입니다.

자바스크립트 1.8가 개발되어지고 있는 상황은 bug 380236을 참조하십시오.

자바스크립트 1.8의 사용

HTML에서 자바스크립트 1.8의 새로운 기능 몇 가지를 사용하려면 다음과 같이 사용하십시오.

 <script type="application/javascript;version=1.8"> ... your code ... </script>

자바스크립트 쉘, 자바스크립트 XPCOM 컴포넌트, 혹은 XUL의 <script> 엘리먼트를 사용할 때에는 자동적으로 가장 마지막의 자바스크립트 버전(모질라 1.9에서는 자바스크립트 1.8)이 사용되어 집니다(bug 381031, bug 385159).

새로운 예약어 "yield"와 "let"를 사용하려면 버전을 1.8이나 그 이상으로 지정하여야 합니다. 왜냐하면 기존 코드에 변수나 함수의 이름으로 이런 키워드를 사용하고 있을 수 있기 때문입니다. (generator expressions처럼) 새로운 예약어로 소개되지 않는 것들은 자바스크립트 버전을 명시하지 않아도 사용할 수 있습니다.

closures 표현

추가된 closures 표현은 간단한 함수 작성을 쉽게 할 수 있는 것 뿐입니다. 문자 그대로인 Lambda notation과 유사한 어떤 것을 자바스크립트도 가능하도록 합니다.

자바스크립트 1.7과 그 이전 버전:

 function(x) { return x * x; }

자바스크립트 1.8:

 function(x) x * x

이런 문법은 괄호를 없애고 'return' 문을 쓰지 않아도 되도록 해줍니다. - 즉, 내용을 함축적으로 만들어 줍니다. 문법이 더 짧아진다는 것 외에 코드에 이 문법을 사용하여 얻을 수 있는 추가적인 이점은 없습니다.

예제:

이벤트 리스너 연결:

 document.addEventListener("click", function() false, true);

자바스크립트 1.6의 배열 함수의 some을 사용하면:

 elems.some(function(elem) elem.type == "text");

Generator expression

Generator expression은 (자바스크립트 1.7에서 소개된) generator를 간단하게 만들 수 있게 해줍니다. 문자 그대로 함수 내부에 yield를 포함한 사용자 함수를 생성할 수 있습니다. 그러나 동일한 generator 문법과 같은 것을 포함하고 있는 배열 역시 사용할 수 있습니다.

This addition allows you to simply create generators (which were introduced in JavaScript 1.7). Typically you would have to create a custom function which would have a yield in it, but this addition allows you to use array comprehension-like syntax to create an identical generator statement.

자바스크립트 1.7에서 오브젝트로부터 사용자 generator를 만드려면 다음과 같은 코드를 작성해야 했습니다:

 function add3(obj) {
   for ( let i in obj )
     yield i + 3;
 }
 
 let it = add3(someObj);
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

위의 코드 대신에 자바스크립트 1.8에서는 generator expression을 사용하여 순환문 안에 사용자 generator 함수를 생성하는 부분을 포함할 수 있습니다:

 let it = (i + 3 for (i in someObj));
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

generator 표현은 또한 값처럼 함수에 전달할 수 있습니다. generator 표현은 generator가 실행되지 않을 때부터 반드시 필요할 때까지 (배열이 맨 처음 생성되어지는 전형적인 상황과는 다르게) 특히 주목할만 합니다. 다른 예는 아래에서 볼 수 있습니다:

Generator expressions can also be passed in, as values, to a function. This is particularly noteworthy since generators aren't run until they are absolutely needed (unlike for typical array comprehension situations, where the arrays are constructed ahead of time). An example of the difference can be seen here:

자바스크립트 1.7 Array Comprehension 사용

 handleResults([ i for ( i in obj ) if ( i > 3 ) ]);
 
 function handleResults( results ) {
   for ( let i in results )
     // ...
 }

자바스크립트 1.8 Generator Expressions 사용

 handleResults( i for ( i in obj ) if ( i > 3 ) );
 
 function handleResults( results ) {
   for ( let i in results )
     // ...
 }

두 예제에서 generator expression을 사용함으로써 중요한 다른 점은 'obj' 하나에 대해서만 반복될 것이란 것과 전체에 대해서 반복될 것이라는 점입니다. 배열에 포함되었을 때 하나에 대해서만 반복될 것에 비해 generator expression은 전체에 대해 계속 반복됩니다.

The significant difference between the two examples being that by using the generator expressions, you would only have to loop over the 'obj' structure once, total, as opposed to once when comprehending the array, and again when iterating through it.

배열에 추가된 것들

자바스크립트 1.8에는 Array 객체에 배열 항목마다 반복 적용되는 두 가지 새로운 메소드가 포함되었습니다:

  • reduce() - 배열의 모든 항목에 대해 함수를 실행하고, 이전 호출에서 결과값을 수집합니다.
  • reduceRight() - 배열의 모든 항목에 대해 함수를 실행하고, 이전 호출에서 결과값을 수집합니다. 결과값은 순서가 반대로 수집됩니다.
  • reduce() - runs a function on every item in the array and collects the results from previous calls.
  • reduceRight() - runs a function on every item in the array and collects the results from previous calls, but in reverse.

Changes in destructuring for..in

TBD: mention New_in_JavaScript_1.7#Looping_across_objects (bug 366941).

곧 변경될 사항들

자바스크립트 1.8에 포함될 수 있을 것이라 기대되는 것들:

문서 태그 및 공헌자

 이 페이지의 공헌자: teoli, Gilchris
 최종 변경: teoli,