diff --git a/files/en-us/web/javascript/reference/global_objects/asyncfunction/asyncfunction/index.md b/files/en-us/web/javascript/reference/global_objects/asyncfunction/asyncfunction/index.md new file mode 100644 index 000000000000000..cd747b1acde670b --- /dev/null +++ b/files/en-us/web/javascript/reference/global_objects/asyncfunction/asyncfunction/index.md @@ -0,0 +1,81 @@ +--- +title: AsyncFunction() constructor +slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction/AsyncFunction +tags: + - Constructor + - JavaScript + - Reference +browser-compat: javascript.builtins.AsyncFunction.AsyncFunction +--- + +{{JSRef}} + +The **`AsyncFunction()`** constructor creates a new {{jsxref("AsyncFunction")}} object. In JavaScript, every [async function](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) is actually an `AsyncFunction` object. + +Note that `AsyncFunction` is _not_ a global object. It can be obtained with the following code: + +```js +const AsyncFunction = async function () {}.constructor; +``` + +The `AsyncFunction()` constructor is not intended to be used directly, and all caveats mentioned in the {{jsxref("Function/Function", "Function()")}} description apply to `AsyncFunction()`. + +## Syntax + +```js-nolint +new AsyncFunction(functionBody) +new AsyncFunction(arg0, functionBody) +new AsyncFunction(arg0, arg1, functionBody) +new AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody) + +AsyncFunction(functionBody) +AsyncFunction(arg0, functionBody) +AsyncFunction(arg0, arg1, functionBody) +AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody) +``` + +> **Note:** `AsyncFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncFunction` instance. + +### Parameters + +See {{jsxref("Function/Function", "Function()")}}. + +## Examples + +### Creating an async function from an AsyncFunction() constructor + +```js +function resolveAfter2Seconds(x) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(x); + }, 2000); + }); +} + +const AsyncFunction = async function () {}.constructor; + +const fn = new AsyncFunction( + "a", + "b", + "return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);", +); + +fn(10, 20).then((v) => { + console.log(v); // prints 30 after 4 seconds +}); +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [`async function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) +- [`async function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function) +- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) diff --git a/files/en-us/web/javascript/reference/global_objects/asyncfunction/index.md b/files/en-us/web/javascript/reference/global_objects/asyncfunction/index.md index 2df6e7d2049051c..ee30edc5d2fb4ca 100644 --- a/files/en-us/web/javascript/reference/global_objects/asyncfunction/index.md +++ b/files/en-us/web/javascript/reference/global_objects/asyncfunction/index.md @@ -10,93 +10,29 @@ browser-compat: javascript.builtins.AsyncFunction {{JSRef}} -The **`AsyncFunction` constructor** creates a new -{{jsxref("Statements/async_function", "async function", "", 1)}} object. In JavaScript, -every asynchronous function is actually an `AsyncFunction` object. +The **`AsyncFunction`** object provides methods for [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function). In JavaScript, every async function is actually an `AsyncFunction` object. -Note that `AsyncFunction` is _not_ a global object. It can be -obtained with the following code: +Note that `AsyncFunction` is _not_ a global object. It can be obtained with the following code: ```js -const AsyncFunction = (async function () {}).constructor; +const AsyncFunction = async function () {}.constructor; ``` -## Syntax +## Constructor -```js-nolint -new AsyncFunction(functionBody) -new AsyncFunction(arg0, functionBody) -new AsyncFunction(arg0, arg1, functionBody) -new AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody) +- {{jsxref("AsyncFunction/AsyncFunction", "AsyncFunction()")}} + - : Creates a new `AsyncFunction` object. -AsyncFunction(functionBody) -AsyncFunction(arg0, functionBody) -AsyncFunction(arg0, arg1, functionBody) -AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody) -``` - -> **Note:** `AsyncFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncFunction` instance. - -### Parameters - -- `argN` {{optional_inline}} - - - : Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain [identifier](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) parameter, optionally with a default), or a list of such strings separated with commas. - - As the parameters are parsed in the same way as function declarations, whitespace and comments are accepted. For example: `"x", "theValue = 42", "[a, b] /* numbers */"` — or `"x, theValue = 42, [a, b] /* numbers */"`. (`"x, theValue = 42", "[a, b]"` is also correct, though very confusing to read.) - -- `functionBody` - - : A string containing the JavaScript statements comprising the function definition. +## Instance properties -## Description +_Also inherits instance properties from its parent {{jsxref("Function")}}_. -[Async function](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) objects created with the -`AsyncFunction` constructor are parsed when the function is created. This is -less efficient than declaring an async function with an -[`async function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function) and calling it -within your code, because such functions are parsed with the rest of the code. +- `AsyncFunction.prototype[@@toStringTag]` + - : The initial value of the [`@@toStringTag`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"AsyncFunction"`. This property is used in {{jsxref("Object.prototype.toString()")}}. -All arguments passed to the function, except the last, 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. +## Instance methods -> **Note:** {{jsxref("Statements/async_function", "async functions", "", 1)}} created with the `AsyncFunction` constructor do not create closures to -> their creation contexts; they are always 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 `AsyncFunction` -> constructor was called. -> -> This is different from using {{jsxref("Global_Objects/eval", "eval")}} with code for -> an async function expression. - -Invoking the `AsyncFunction` constructor as a function (without using the -`new` operator) has the same effect as invoking it as a constructor. - -## Examples - -### Creating an async function from an AsyncFunction() constructor - -```js -function resolveAfter2Seconds(x) { - return new Promise((resolve) => { - setTimeout(() => { - resolve(x); - }, 2000); - }); -} - -const AsyncFunction = (async function () {}).constructor; - -const fn = new AsyncFunction( - 'a', - 'b', - 'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);', -); - -fn(10, 20).then((v) => { - console.log(v); // prints 30 after 4 seconds -}); -``` +_Inherits instance methods from its parent {{jsxref("Function")}}_. ## Specifications @@ -108,9 +44,9 @@ fn(10, 20).then((v) => { ## See also -- {{jsxref("Statements/async_function", "async function", "", 1)}} -- {{jsxref("Operators/async_function", "async function expression", "", 1)}} -- {{jsxref("Global_Objects/Function", "Function")}} -- {{jsxref("Statements/function", "function statement", "", 1)}} -- {{jsxref("Operators/function", "function expression", "", 1)}} -- {{jsxref("Functions", "Functions and function scope", "", 1)}} +- [`async function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) +- [`async function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function) +- {{jsxref("Function")}} +- {{jsxref("AsyncGeneratorFunction")}} +- {{jsxref("GeneratorFunction")}} +- {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/asyncgeneratorfunction/index.md b/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/asyncgeneratorfunction/index.md new file mode 100644 index 000000000000000..371d14dae88fe72 --- /dev/null +++ b/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/asyncgeneratorfunction/index.md @@ -0,0 +1,73 @@ +--- +title: AsyncGeneratorFunction() constructor +slug: Web/JavaScript/Reference/Global_Objects/AsyncGeneratorFunction/AsyncGeneratorFunction +tags: + - Constructor + - ECMAScript 2018 + - AsyncGeneratorFunction + - Iterator + - JavaScript + - Reference +browser-compat: javascript.builtins.AsyncGeneratorFunction.AsyncGeneratorFunction +--- + +{{JSRef}} + +The **`AsyncGeneratorFunction()`** constructor creates a new {{jsxref("AsyncGeneratorFunction")}} object. In JavaScript, every [async generator function](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) is actually an `AsyncGeneratorFunction` object. + +Note that `AsyncGeneratorFunction` is not a global object. It could be obtained by evaluating the following code. + +```js +const AsyncGeneratorFunction = async function* () {}.constructor; +``` + +The `AsyncGeneratorFunction()` constructor is not intended to be used directly, and all caveats mentioned in the {{jsxref("Function/Function", "Function()")}} description apply to `AsyncGeneratorFunction()`. + +## Syntax + +```js-nolint +new AsyncGeneratorFunction(functionBody) +new AsyncGeneratorFunction(arg0, functionBody) +new AsyncGeneratorFunction(arg0, arg1, functionBody) +new AsyncGeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) + +AsyncGeneratorFunction(functionBody) +AsyncGeneratorFunction(arg0, functionBody) +AsyncGeneratorFunction(arg0, arg1, functionBody) +AsyncGeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) +``` + +> **Note:** `AsyncGeneratorFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncGeneratorFunction` instance. + +### Parameters + +See {{jsxref("Function/Function", "Function()")}}. + +## Examples + +### Using the constructor + +The following example uses the `AsyncGeneratorFunction` constructor to create an async generator function. + +```js +const AsyncGeneratorFunction = async function* () {}.constructor; +const createAsyncGenerator = new AsyncGeneratorFunction("a", "yield a * 2"); +const asyncGen = createAsyncGenerator(10); +asyncGen.next().then((res) => console.log(res.value)); // 20 +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) +- [`async function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function*) +- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) +- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) +- {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/index.md b/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/index.md index 32cb1f3f3f2ac67..0bf28f5e8399512 100644 --- a/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/index.md +++ b/files/en-us/web/javascript/reference/global_objects/asyncgeneratorfunction/index.md @@ -1,79 +1,36 @@ --- title: AsyncGeneratorFunction slug: Web/JavaScript/Reference/Global_Objects/AsyncGeneratorFunction -tags: -- Constructor -- ECMAScript 2018 -- AsyncGeneratorFunction -- Iterator -- JavaScript -- Reference browser-compat: javascript.builtins.AsyncGeneratorFunction --- {{JSRef}} -The **`AsyncGeneratorFunction` constructor** creates a new {{jsxref("Statements/async_function*", "async generator function", "", 1)}} object. In JavaScript, every async generator function is actually an `AsyncGeneratorFunction` object. +The **`AsyncGeneratorFunction`** object provides methods for [async generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). In JavaScript, every async generator function is actually an `AsyncGeneratorFunction` object. -Note that `AsyncGeneratorFunction` is not a global object. It could be obtained by evaluating the following code. +Note that `AsyncGeneratorFunction` is _not_ a global object. It can be obtained with the following code: ```js -const AsyncGeneratorFunction = (async function* () {}).constructor; +const AsyncGeneratorFunction = async function* () {}.constructor; ``` -## Syntax +## Constructor -```js-nolint -new AsyncGeneratorFunction(functionBody) -new AsyncGeneratorFunction(arg0, functionBody) -new AsyncGeneratorFunction(arg0, arg1, functionBody) -new AsyncGeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) +- {{jsxref("AsyncGeneratorFunction/AsyncGeneratorFunction", "AsyncGeneratorFunction()")}} + - : Creates a new `AsyncGeneratorFunction` object. -AsyncGeneratorFunction(functionBody) -AsyncGeneratorFunction(arg0, functionBody) -AsyncGeneratorFunction(arg0, arg1, functionBody) -AsyncGeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) -``` - -> **Note:** `AsyncGeneratorFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncGeneratorFunction` instance. - -### Parameters - -- `argN` {{optional_inline}} - - - : Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain [identifier](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) parameter, optionally with a default), or a list of such strings separated with commas. - - As the parameters are parsed in the same way as function declarations, whitespace and comments are accepted. For example: `"x", "theValue = 42", "[a, b] /* numbers */"` — or `"x, theValue = 42, [a, b] /* numbers */"`. (`"x, theValue = 42", "[a, b]"` is also correct, though very confusing to read.) - -- `functionBody` - - : A {{jsxref("String")}} containing the JavaScript statements comprising the function definition. +## Instance properties -## Description +_Also inherits instance properties from its parent {{jsxref("Function")}}_. -Async generator function objects created with the `AsyncGeneratorFunction` constructor are parsed when the function is created. This is less efficient than declaring a generator function with an [`async function*` expression](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) and calling it within your code, because such functions are parsed with the rest of the code. +- `AsyncGeneratorFunction.prototype[@@toStringTag]` + - : The initial value of the [`@@toStringTag`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"AsyncGeneratorFunction"`. This property is used in {{jsxref("Object.prototype.toString()")}}. +- `AsyncGeneratorFunction.prototype.prototype` + - : All async generator functions share the same [`prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) property, which is [`AsyncGenerator.prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator). When the function is called, this object becomes the prototype of the returned async generator object. An async generator function instance can also create its own `prototype` property, which will be used instead of `AsyncGeneratorFunction.prototype.prototype`. -All arguments passed to the function, except the last, 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. +## Instance methods -> **Note:** Async generator functions created with the `AsyncGeneratorFunction` constructor do not create closures to their creation contexts; they are always 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 `AsyncGeneratorFunction` constructor was called. -> -> This is different from using {{jsxref("Global_Objects/eval", "eval")}} with code for an async generator function expression. - -Invoking the `AsyncGeneratorFunction` constructor as a function (without using the `new` operator) has the same effect as invoking it as a constructor. - -## Examples - -### Using the constructor - -The following example uses the `AsyncGeneratorFunction` constructor to create an async generator function. - -```js -const AsyncGeneratorFunction = (async function* () {}).constructor; -const createAsyncGenerator = new AsyncGeneratorFunction('a', 'yield a * 2'); -const asyncGen = createAsyncGenerator(10); -asyncGen.next().then((res) => console.log(res.value)); // 20 -``` +_Inherits instance methods from its parent {{jsxref("Function")}}_. ## Specifications @@ -85,12 +42,9 @@ asyncGen.next().then((res) => console.log(res.value)); // 20 ## See also -- {{jsxref("Statements/function*", "function*")}} -- {{jsxref("Statements/async_function*", "async function*")}} -- [`function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function*) -- {{jsxref("Global_Objects/AsyncGenerator", "AsyncGenerator")}} -- {{jsxref("Global_Objects/Generator", "Generator")}} -- {{jsxref("Global_Objects/GeneratorFunction", "GeneratorFunction")}} -- {{jsxref("Global_Objects/AsyncFunction", "AsyncFunction")}} -- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) +- [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) +- [`async function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function*) +- {{jsxref("Function")}} +- {{jsxref("AsyncFunction")}} +- {{jsxref("GeneratorFunction")}} - {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/function/function/index.md b/files/en-us/web/javascript/reference/global_objects/function/function/index.md index 1181f4f16a1f3ea..c90f35a762c0560 100644 --- a/files/en-us/web/javascript/reference/global_objects/function/function/index.md +++ b/files/en-us/web/javascript/reference/global_objects/function/function/index.md @@ -135,10 +135,6 @@ sayHello('world'); ## See also -- {{jsxref("Functions", "Functions and function scope", "", 1)}} -- {{jsxref("Statements/function", "function")}} statement -- {{jsxref("Operators/function", "function")}} expression -- {{jsxref("Statements/function*", "function*")}} statement -- {{jsxref("Operators/function*", "function*")}} expression -- {{jsxref("AsyncFunction")}} -- {{jsxref("GeneratorFunction")}} +- [`function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/function) +- [`function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function) +- {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/function/index.md b/files/en-us/web/javascript/reference/global_objects/function/index.md index 67b837d253f151b..9fe93ab98d9358a 100644 --- a/files/en-us/web/javascript/reference/global_objects/function/index.md +++ b/files/en-us/web/javascript/reference/global_objects/function/index.md @@ -87,10 +87,9 @@ While this code works in web browsers, `f1()` will produce a `ReferenceError` in ## See also -- {{jsxref("Functions", "Functions and function scope", "", 1)}} -- {{jsxref("Statements/function", "function")}} statement -- {{jsxref("Operators/function", "function")}} expression -- {{jsxref("Statements/function*", "function*")}} statement -- {{jsxref("Operators/function*", "function*")}} expression +- [`function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/function) +- [`function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function) - {{jsxref("AsyncFunction")}} +- {{jsxref("AsyncGeneratorFunction")}} - {{jsxref("GeneratorFunction")}} +- {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/function/prototype/index.md b/files/en-us/web/javascript/reference/global_objects/function/prototype/index.md index ca822ceeb5ef891..708d8610b341932 100644 --- a/files/en-us/web/javascript/reference/global_objects/function/prototype/index.md +++ b/files/en-us/web/javascript/reference/global_objects/function/prototype/index.md @@ -18,6 +18,8 @@ A {{jsxref("Function")}} object's **`prototype`** property is used when the func > **Note:** The `prototype` property of [classes](/en-US/docs/Web/JavaScript/Reference/Classes) is not writable. +> **Note:** The `prototype` properties of [generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/function*) and [async generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) are not writable but are configurable. + ## Description When a function is called with [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new), the constructor's `prototype` property will become the resulting object's prototype. diff --git a/files/en-us/web/javascript/reference/global_objects/generatorfunction/generatorfunction/index.md b/files/en-us/web/javascript/reference/global_objects/generatorfunction/generatorfunction/index.md new file mode 100644 index 000000000000000..da979c516a7078d --- /dev/null +++ b/files/en-us/web/javascript/reference/global_objects/generatorfunction/generatorfunction/index.md @@ -0,0 +1,68 @@ +--- +title: GeneratorFunction() constructor +slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction/GeneratorFunction +tags: + - Constructor + - JavaScript + - Reference +browser-compat: javascript.builtins.GeneratorFunction.GeneratorFunction +--- + +{{JSRef}} + +The **`GeneratorFunction()`** constructor creates a new {{jsxref("GeneratorFunction")}} object. In JavaScript, every [generator function](/en-US/docs/Web/JavaScript/Reference/Statements/function*) is actually a `GeneratorFunction` object. + +Note that `GeneratorFunction` is _not_ a global object. It can be obtained with the following code: + +```js +const GeneratorFunction = function* () {}.constructor; +``` + +The `GeneratorFunction()` constructor is not intended to be used directly, and all caveats mentioned in the {{jsxref("Function/Function", "Function()")}} description apply to `GeneratorFunction()`. + +## Syntax + +```js-nolint +new GeneratorFunction(functionBody) +new GeneratorFunction(arg0, functionBody) +new GeneratorFunction(arg0, arg1, functionBody) +new GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) + +GeneratorFunction(functionBody) +GeneratorFunction(arg0, functionBody) +GeneratorFunction(arg0, arg1, functionBody) +GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) +``` + +> **Note:** `GeneratorFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `GeneratorFunction` instance. + +### Parameters + +See {{jsxref("Function/Function", "Function()")}}. + +## Examples + +### Creating and using a GeneratorFunction() constructor + +```js +const GeneratorFunction = function* () {}.constructor; +const g = new GeneratorFunction("a", "yield a * 2"); +const iterator = g(10); +console.log(iterator.next().value); // 20 +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [`function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/function*) +- [`function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function*) +- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) +- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) +- {{jsxref("Functions", "Functions", "", 1)}} diff --git a/files/en-us/web/javascript/reference/global_objects/generatorfunction/index.md b/files/en-us/web/javascript/reference/global_objects/generatorfunction/index.md index a7d04d0383f00bc..6b8f6f5b46c5d9a 100644 --- a/files/en-us/web/javascript/reference/global_objects/generatorfunction/index.md +++ b/files/en-us/web/javascript/reference/global_objects/generatorfunction/index.md @@ -19,68 +19,23 @@ In JavaScript, every generator function is actually a `GeneratorFunction` object const GeneratorFunction = function* () {}.constructor; ``` -## Syntax - -```js-nolint -new GeneratorFunction(functionBody) -new GeneratorFunction(arg0, functionBody) -new GeneratorFunction(arg0, arg1, functionBody) -new GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) - -GeneratorFunction(functionBody) -GeneratorFunction(arg0, functionBody) -GeneratorFunction(arg0, arg1, functionBody) -GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody) -``` - -> **Note:** `GeneratorFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `GeneratorFunction` instance. - -### Parameters - -- `argN` {{optional_inline}} - - - : Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain [identifier](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) parameter, optionally with a default), or a list of such strings separated with commas. - - As the parameters are parsed in the same way as function declarations, whitespace and comments are accepted. For example: `"x", "theValue = 42", "[a, b] /* numbers */"` — or `"x, theValue = 42, [a, b] /* numbers */"`. (`"x, theValue = 42", "[a, b]"` is also correct, though very confusing to read.) +## Constructor -- `functionBody` - - : A {{jsxref("String")}} containing the JavaScript statements comprising the function definition. +- {{jsxref("GeneratorFunction/GeneratorFunction", "GeneratorFunction()")}} + - : Creates a new `GeneratorFunction` object. -## Description +## Instance properties -{{jsxref("Statements/function*", "Generator function", "", "1")}} objects created with a -constructor are parsed when the function is created. That -is less efficient than declaring a generator function with a -[`function*` expression](/en-US/docs/Web/JavaScript/Reference/Statements/function*) and calling it within your -code, because such functions are parsed with the rest of the code. +_Also inherits instance properties from its parent {{jsxref("Function")}}_. -All arguments passed to the function, except the last, 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. +- `GeneratorFunction.prototype[@@toStringTag]` + - : The initial value of the [`@@toStringTag`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"GeneratorFunction"`. This property is used in {{jsxref("Object.prototype.toString()")}}. +- `GeneratorFunction.prototype.prototype` + - : All generator functions share the same [`prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) property, which is [`Generator.prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator). When the function is called, this object becomes the prototype of the returned generator object. A generator function instance can also create its own `prototype` property, which will be used instead of `GeneratorFunction.prototype.prototype`. -> **Note:** {{jsxref("Statements/function*", "generator functions", "", "1")}} -> created with a constructor do not create closures to -> their creation contexts; they are always 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 {{jsxref("Global_Objects/eval", "eval")}} with code for -> a generator function expression. +## Instance methods -Invoking a generator function constructor as a function (without using -the `new` operator) has the same effect as invoking it as a constructor. - -## Examples - -### Creating and using a GeneratorFunction() constructor - -```js -const GeneratorFunction = function* () {}.constructor; -const g = new GeneratorFunction("a", "yield a * 2"); -const iterator = g(10); -console.log(iterator.next().value); // 20 -``` +_Inherits instance methods from its parent {{jsxref("Function")}}_. ## Specifications @@ -92,9 +47,9 @@ console.log(iterator.next().value); // 20 ## See also -- {{jsxref("Statements/function*", "function* function", "", 1)}} -- {{jsxref("Operators/function*", "function* expression", "", 1)}} -- {{jsxref("Global_Objects/Function", "Function")}} -- {{jsxref("Statements/function", "function statement", "", 1)}} -- {{jsxref("Operators/function", "function expression", "", 1)}} -- {{jsxref("Functions", "Functions and function scope", "", 1)}} +- [`function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/function*) +- [`function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function*) +- {{jsxref("Function")}} +- {{jsxref("AsyncFunction")}} +- {{jsxref("AsyncGeneratorFunction")}} +- {{jsxref("Functions", "Functions", "", 1)}}