We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

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

GeneratorFunction 생성자 는 새로운 generator function 객체를 생성한다. JavaScript 에서 모든 generator function 은 실제로 GeneratorFunction object 이다.

주의할 점은, GeneratorFunction 이 전역 객체(global object)가 아니란 점이다. GeneratorFunction은 다음의 코드를 실행해서 얻을 수 있다.



new GeneratorFunction ([arg1[, arg2[, ...argN]],] functionBody)


arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
A string containing the JavaScript statements comprising the function definition.


generator function objects created with the GeneratorFunction constructor are parsed when the function is created. This is less efficient than declaring a generator function with a function* expression and calling it within your code, because such functions are parsed with the rest of the code.

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Note: generator function created with the GeneratorFunction constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the GeneratorFunction constructor was called. This is different from using eval with code for a generator function expression.

new 없이 GeneratorFunction 생성자를 함수처럼 사용하는 것은 생성자처럼 사용하는 것과 동일한 효과를 갖는다.


GeneratorFunction 생성자의 length 속성으로 1 값을 갖는다.
모든 generator function objects 에 속성의 추가를 허용한다.

GeneratorFunction prototype object


The initial value is GeneratorFunction.
The value is %GeneratorPrototype%.

GeneratorFunction instances

GeneratorFunction 인스턴스는 GeneratorFunction.prototype 으로부터 속성과 메서드를 상속한다. 다른 모든 생성자들처럼, 생성자의 prototype object 를 변경함으로써 모든 GeneratorFunction instance 에 변화를 줄 수 있다.


GeneratorFunction 생성자로 generator function 생성하기

var GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor
var g = new GeneratorFunction("a", "yield a * 2");
var iterator = g(10);
console.log(iterator.next().value); // 20


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'GeneratorFunction' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'GeneratorFunction' in that specification.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 26 (26) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) 26 (26) ? ? ?

See also

문서 태그 및 공헌자

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