diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/my/web/javascript/reference | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/my/web/javascript/reference')
3 files changed, 694 insertions, 0 deletions
diff --git a/files/my/web/javascript/reference/index.html b/files/my/web/javascript/reference/index.html new file mode 100644 index 0000000000..1345585f85 --- /dev/null +++ b/files/my/web/javascript/reference/index.html @@ -0,0 +1,312 @@ +--- +title: JavaScript reference +slug: Web/JavaScript/Reference +tags: + - Code + - ECMAScript + - ECMAScript6 + - ES6 + - JS + - JavaScript + - Landing page + - NeedsTranslation + - Reference + - TopicStub + - es + - 'l10n:priority' + - programming +translation_of: Web/JavaScript/Reference +--- +<div>{{JsSidebar}}</div> + +<p>This part of the JavaScript section on MDN serves as a repository of facts about the JavaScript language. Read more <a href="/en-US/docs/Web/JavaScript/Reference/About">about this reference</a>.</p> + +<h2 id="Built-ins">Built-ins</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects">JavaScript standard built-in objects</a>, along with their methods and properties.</p> + +<ul class="card-grid"> + <li><span>Value properties</span> + + <p>{{JSxRef("Infinity")}}<br> + {{JSxRef("NaN")}}<br> + {{JSxRef("undefined")}}<br> + {{JSxRef("globalThis")}}</p> + </li> + <li><span>Function properties</span> + <p>{{JSxRef("Global_Objects/eval", "eval()")}}<br> + {{JSxRef("Global_Objects/isFinite", "isFinite()")}}<br> + {{JSxRef("Global_Objects/isNaN", "isNaN()")}}<br> + {{JSxRef("Global_Objects/parseFloat", "parseFloat()")}}<br> + {{JSxRef("Global_Objects/parseInt", "parseInt()")}}<br> + {{JSxRef("Global_Objects/decodeURI", "decodeURI()")}}<br> + {{JSxRef("Global_Objects/decodeURIComponent", "decodeURIComponent()")}}<br> + {{JSxRef("Global_Objects/encodeURI", "encodeURI()")}}<br> + {{JSxRef("Global_Objects/encodeURIComponent", "encodeURIComponent()")}}</p> + </li> + <li><span>Fundamental objects</span> + <p>{{JSxRef("Object")}}<br> + {{JSxRef("Function")}}<br> + {{JSxRef("Boolean")}}<br> + {{JSxRef("Symbol")}}</p> + </li> + <li><span>Error objects</span> + <p>{{JSxRef("Error")}}<br> + {{JSxRef("AggregateError")}}<br> + {{JSxRef("EvalError")}}<br> + {{JSxRef("InternalError")}}<br> + {{JSxRef("RangeError")}}<br> + {{JSxRef("ReferenceError")}}<br> + {{JSxRef("SyntaxError")}}<br> + {{JSxRef("TypeError")}}<br> + {{JSxRef("URIError")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>Numbers & dates</span> + + <p>{{JSxRef("Number")}}<br> + {{JSxRef("BigInt")}}<br> + {{JSxRef("Math")}}<br> + {{JSxRef("Date")}}</p> + </li> + <li><span>Text processing</span> + <p>{{JSxRef("String")}}<br> + {{JSxRef("RegExp")}}</p> + </li> + <li><span>Indexed Collections</span><a href="/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates#Numbers"> </a>{{JSxRef("Array")}}<br> + {{JSxRef("Int8Array")}}<br> + {{JSxRef("Uint8Array")}}<br> + {{JSxRef("Uint8ClampedArray")}}<br> + {{JSxRef("Int16Array")}}<br> + {{JSxRef("Uint16Array")}}<br> + {{JSxRef("Int32Array")}}<br> + {{JSxRef("Uint32Array")}}<br> + {{JSxRef("Float32Array")}}<br> + {{JSxRef("Float64Array")}}<br> + {{JSxRef("BigInt64Array")}}<br> + {{JSxRef("BigUint64Array")}}</li> + <li><span>Keyed collections</span> + <p>{{JSxRef("Map")}}<br> + {{JSxRef("Set")}}<br> + {{JSxRef("WeakMap")}}<br> + {{JSxRef("WeakSet")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>Structured data</span> + + <p>{{JSxRef("ArrayBuffer")}}<br> + {{JSxRef("SharedArrayBuffer")}}<br> + {{JSxRef("Atomics")}}<br> + {{JSxRef("DataView")}}<br> + {{JSxRef("JSON")}}</p> + </li> + <li><span>Control abstraction</span> + <p>{{JSxRef("Promise")}}<br> + {{JSxRef("Generator")}}<br> + {{JSxRef("GeneratorFunction")}}<br> + {{JSxRef("AsyncFunction")}}</p> + </li> + <li><span>Reflection</span> + <p>{{JSxRef("Reflect")}}<br> + {{JSxRef("Proxy")}}</p> + </li> + <li><span>Internationalization</span> + <p>{{JSxRef("Intl")}}<br> + {{JSxRef("Global_Objects/Intl/Collator", "Intl.Collator")}}<br> + {{JSxRef("Global_Objects/Intl/DateTimeFormat", "Intl.DateTimeFormat")}}<br> + {{JSxRef("Global_Objects/Intl/DisplayNames", "Intl.DisplayNames")}}<br> + {{JSxRef("Global_Objects/Intl/ListFormat", "Intl.ListFormat")}}<br> + {{JSxRef("Global_Objects/Intl/Locale", "Intl.Locale")}}<br> + {{JSxRef("Global_Objects/Intl/NumberFormat", "Intl.NumberFormat")}}<br> + {{JSxRef("Global_Objects/Intl/PluralRules", "Intl.PluralRules")}}<br> + {{JSxRef("Global_Objects/Intl/RelativeTimeFormat", "Intl.RelativeTimeFormat")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>WebAssembly</span> + + <p>{{JSxRef("WebAssembly")}}<br> + {{JSxRef("WebAssembly.Module")}}<br> + {{JSxRef("WebAssembly.Instance")}}<br> + {{JSxRef("WebAssembly.Memory")}}<br> + {{JSxRef("WebAssembly.Table")}}<br> + {{JSxRef("WebAssembly.CompileError")}}<br> + {{JSxRef("WebAssembly.LinkError")}}<br> + {{JSxRef("WebAssembly.RuntimeError")}}</p> + </li> +</ul> + +<h2 id="Statements">Statements</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript statements and declarations</a></p> + +<ul class="card-grid"> + <li><span>Control flow</span>{{jsxref("Statements/block", "Block")}}<br> + {{jsxref("Statements/break", "break")}}<br> + {{jsxref("Statements/continue", "continue")}}<br> + {{jsxref("Statements/Empty", "Empty")}}<br> + {{jsxref("Statements/if...else", "if...else")}}<br> + {{jsxref("Statements/switch", "switch")}}<br> + {{jsxref("Statements/throw", "throw")}}<br> + {{jsxref("Statements/try...catch", "try...catch")}}</li> + <li><span>Declarations</span> + <p>{{jsxref("Statements/var", "var")}}<br> + {{jsxref("Statements/let", "let")}}<br> + {{jsxref("Statements/const", "const")}}</p> + </li> + <li><span>Functions and classes</span> + <p>{{jsxref("Statements/function", "function")}}<br> + {{jsxref("Statements/function*", "function*")}}<br> + {{jsxref("Statements/async_function", "async function")}}<br> + {{jsxref("Statements/return", "return")}}<br> + {{jsxref("Statements/class", "class")}}</p> + </li> + <li><span>Iterations</span> + <p>{{jsxref("Statements/do...while", "do...while")}}<br> + {{jsxref("Statements/for", "for")}}<br> + {{jsxref("Statements/for_each...in", "for each...in")}}<br> + {{jsxref("Statements/for...in", "for...in")}}<br> + {{jsxref("Statements/for...of", "for...of")}}<br> + {{jsxref("Statements/for-await...of", "for await...of")}}<br> + {{jsxref("Statements/while", "while")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>Other</span> + + <p>{{jsxref("Statements/debugger", "debugger")}}<br> + {{jsxref("Statements/import", "import")}}<br> + {{jsxref("Statements/label", "label")}}<br> + {{jsxref("Statements/with", "with")}}</p> + </li> +</ul> + +<h2 id="Expressions_and_operators">Expressions and operators</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Operators">JavaScript expressions and operators</a>. </p> + +<div> +<ul class="card-grid"> + <li><span>Primary expressions</span>{{JSxRef("Operators/this", "this")}}<br> + {{JSxRef("Operators/function", "function")}}<br> + {{JSxRef("Operators/class", "class")}}<br> + {{JSxRef("Operators/function*", "function*")}}<br> + {{JSxRef("Operators/yield", "yield")}}<br> + {{JSxRef("Operators/yield*", "yield*")}}<br> + {{JSxRef("Operators/async_function", "async function")}}<br> + {{JSxRef("Operators/await", "await")}}<br> + {{JSxRef("Global_Objects/Array", "[]")}}<br> + {{JSxRef("Operators/Object_initializer", "{}")}}<br> + {{JSxRef("Global_Objects/RegExp", "/ab+c/i")}}<br> + {{JSxRef("Operators/Grouping", "( )")}}<br> + {{JSxRef("null")}}</li> + <li><span>Left-hand-side expressions</span> + <p>{{JSxRef("Operators/Property_accessors", "Property accessors", "", 1)}}<br> + {{JSxRef("Operators/new", "new")}}<br> + {{JSxRef("Operators/new%2Etarget", "new.target")}}<br> + {{JSxRef("Operators/super", "super")}}<br> + {{JSxRef("Operators/Spread_syntax", "...obj")}}</p> + </li> + <li><span>Increment & decrement</span> + <p>{{JSxRef("Operators/Arithmetic_Operators", "A++", "#Increment")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "A--", "#Decrement")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "++A", "#Increment")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "--A", "#Decrement")}}</p> + </li> + <li><span>Unary operators</span> + <p>{{JSxRef("Operators/delete", "delete")}}<br> + {{JSxRef("Operators/void", "void")}}<br> + {{JSxRef("Operators/typeof", "typeof")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "+", "#Unary_plus")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "-", "#Unary_negation")}}<br> + {{JSxRef("Operators/Bitwise_Operators", "~", "#Bitwise_NOT")}}<br> + {{JSxRef("Operators/Logical_Operators", "!", "#Logical_NOT")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>Arithmetic operators</span> + + <p>{{JSxRef("Operators/Arithmetic_Operators", "+", "#Addition")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "-", "#Subtraction")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "/", "#Division")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "*", "#Multiplication")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "%", "#Remainder")}}<br> + {{JSxRef("Operators/Arithmetic_Operators", "**", "#Exponentiation")}}</p> + </li> + <li><span>Relational operators</span> + <p>{{JSxRef("Operators/in", "in")}}<br> + {{JSxRef("Operators/instanceof", "instanceof")}}<br> + {{JSxRef("Operators/Comparison_Operators", "<", "#Less_than_operator")}}<br> + {{JSxRef("Operators/Comparison_Operators", ">", "#Greater_than_operator")}}<br> + {{JSxRef("Operators/Comparison_Operators", "<=", "#Less_than_or_equal_operator")}}<br> + {{JSxRef("Operators/Comparison_Operators", ">=", "#Greater_than_or_equal_operator")}}</p> + </li> + <li><span>Equality operators</span> + <p>{{JSxRef("Operators/Comparison_Operators", "==", "#Equality")}}<br> + {{JSxRef("Operators/Comparison_Operators", "!=", "#Inequality")}}<br> + {{JSxRef("Operators/Comparison_Operators", "===", "#Identity")}}<br> + {{JSxRef("Operators/Comparison_Operators", "!==", "#Nonidentity")}}</p> + </li> + <li><span>Bitwise shift operators</span> + <p>{{JSxRef("Operators/Bitwise_Operators", "<<", "#Left_shift")}}<br> + {{JSxRef("Operators/Bitwise_Operators", ">>", "#Right_shift")}}<br> + {{JSxRef("Operators/Bitwise_Operators", ">>>", "#Unsigned_right_shift")}}</p> + </li> +</ul> + +<ul class="card-grid"> + <li><span>Binary bitwise operators</span>{{JSxRef("Operators/Bitwise_Operators", "&", "#Bitwise_AND")}}<br> + {{JSxRef("Operators/Bitwise_Operators", "|", "#Bitwise_OR")}}<br> + {{JSxRef("Operators/Bitwise_Operators", "^", "#Bitwise_XOR")}}</li> + <li><span>Binary logical operators</span> + <p>{{JSxRef("Operators/Logical_Operators", "&&", "#Logical_AND")}}<br> + {{JSxRef("Operators/Logical_Operators", "||", "#Logical_OR")}}</p> + </li> + <li><span>Conditional (ternary) operator</span> + <p>{{JSxRef("Operators/Conditional_Operator", "(condition ? ifTrue : ifFalse)")}}</p> + </li> + <li><span>Assignment operators</span> + <p>{{JSxRef("Operators/Assignment_Operators", "=", "#Assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "*=", "#Multiplication_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "/=", "#Division_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "%=", "#Remainder_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "+=", "#Addition_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "-=", "#Subtraction_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "<<=", "#Left_shift_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", ">>=", "#Right_shift_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", ">>>=", "#Unsigned_right_shift_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "&=", "#Bitwise_AND_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "^=", "#Bitwise_XOR_assignment")}}<br> + {{JSxRef("Operators/Assignment_Operators", "|=", "#Bitwise_OR_assignment")}}<br> + {{JSxRef("Operators/Destructuring_assignment", "[a, b] = [1, 2]")}}<br> + {{JSxRef("Operators/Destructuring_assignment", "{a, b} = {a:1, b:2}")}}</p> + </li> +</ul> +</div> + +<h2 id="Functions">Functions</h2> + +<p>This chapter documents how to work with <a href="/en-US/docs/Web/JavaScript/Reference/Functions">JavaScript functions</a> to develop your applications.</p> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments"><code>arguments</code></a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters">Default parameters</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">Rest parameters</a></li> +</ul> + +<h2 id="Additional_reference_pages">Additional reference pages</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar">Lexical grammar</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Data_structures">Data types and data structures</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features">Deprecated features</a></li> +</ul> diff --git a/files/my/web/javascript/reference/statements/function_star_/index.html b/files/my/web/javascript/reference/statements/function_star_/index.html new file mode 100644 index 0000000000..414d5f4d63 --- /dev/null +++ b/files/my/web/javascript/reference/statements/function_star_/index.html @@ -0,0 +1,252 @@ +--- +title: function* +slug: Web/JavaScript/Reference/Statements/function* +translation_of: Web/JavaScript/Reference/Statements/function* +--- +<div dir="rtl">{{jsSidebar("Statements")}}</div> + +<p>The <code><strong>function*</strong></code> declaration (<code>function</code> keyword followed by an asterisk) defines a <em>generator function</em>, which returns a {{jsxref("Global_Objects/Generator","Generator")}} object.</p> + +<div>{{EmbedInteractiveExample("pages/js/statement-functionasterisk.html")}}</div> + + + +<p>You can also define generator functions using the {{jsxref("GeneratorFunction")}} constructor, or the function expression syntax.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate">function* <var>name</var>([<var>param</var>[, <var>param</var>[, ... <var>param</var>]]]) { + <var>statements</var> +} +</pre> + +<dl> + <dt><code><var>name</var></code></dt> + <dd>The function name.</dd> + <dt><code><var>param</var></code> {{optional_inline}}</dt> + <dd>The name of a formal parameter for the function.</dd> + <dt><code><var>statements</var></code></dt> + <dd>The statements comprising the body of the function.</dd> +</dl> + +<h2 id="Description">Description</h2> + +<p>Generators are functions that can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.</p> + +<p>Generators in JavaScript -- especially when combined with Promises -- are a very powerful tool for asynchronous programming as they mitigate -- if not entirely eliminate -- the problems with callbacks, such as <a href="http://callbackhell.com/">Callback Hell</a> and <a href="https://frontendmasters.com/courses/rethinking-async-js/callback-problems-inversion-of-control/">Inversion of Control</a>. However, an even simpler solution to these problems can be achieved with {{jsxref("Statements/async_function", "async functions")}}.</p> + +<p>Calling a generator function does not execute its body immediately; an <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterator">iterator</a> object for the function is returned instead. When the iterator's <code>next()</code> method is called, the generator function's body is executed until the first {{jsxref("Operators/yield", "yield")}} expression, which specifies the value to be returned from the iterator or, with {{jsxref("Operators/yield*", "yield*")}}, delegates to another generator function. The <code>next()</code> method returns an object with a <code>value</code> property containing the yielded value and a <code>done</code> property which indicates whether the generator has yielded its last value, as a boolean. Calling the <code>next()</code> method with an argument will resume the generator function execution, replacing the <code>yield</code> expression where an execution was paused with the argument from <code>next()</code>.</p> + +<p>A <code>return</code> statement in a generator, when executed, will make the generator finish (i.e. the <code>done</code> property of the object returned by it will be set to <code>true</code>). If a value is returned, it will be set as the <code>value</code> property of the object returned by the generator.<br> + Much like a <code>return</code> statement, an error is thrown inside the generator will make the generator finished -- unless caught within the generator's body.<br> + When a generator is finished, subsequent <code>next()</code> calls will not execute any of that generator's code, they will just return an object of this form: <code>{value: undefined, done: true}</code>.</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Simple_example">Simple example</h3> + +<pre class="brush: js notranslate">function* idMaker() { + var index = 0; + while (true) + yield index++; +} + +var gen = idMaker(); + +console.log(gen.next().value); // 0 +console.log(gen.next().value); // 1 +console.log(gen.next().value); // 2 +console.log(gen.next().value); // 3 +// ...</pre> + +<h3 id="Example_with_yield*">Example with yield*</h3> + +<pre class="brush: js notranslate">function* anotherGenerator(i) { + yield i + 1; + yield i + 2; + yield i + 3; +} + +function* generator(i) { + yield i; + yield* anotherGenerator(i); + yield i + 10; +} + +var gen = generator(10); + +console.log(gen.next().value); // 10 +console.log(gen.next().value); // 11 +console.log(gen.next().value); // 12 +console.log(gen.next().value); // 13 +console.log(gen.next().value); // 20 +</pre> + +<h3 id="Passing_arguments_into_Generators">Passing arguments into Generators</h3> + +<pre class="brush: js notranslate">function* logGenerator() { + console.log(0); + console.log(1, yield); + console.log(2, yield); + console.log(3, yield); +} + +var gen = logGenerator(); + +// the first call of next executes from the start of the function +// until the first yield statement +gen.next(); // 0 +gen.next('pretzel'); // 1 pretzel +gen.next('california'); // 2 california +gen.next('mayonnaise'); // 3 mayonnaise +</pre> + +<h3 id="Return_statement_in_a_generator">Return statement in a generator</h3> + +<pre class="brush: js notranslate">function* yieldAndReturn() { + yield "Y"; + return "R"; + yield "unreachable"; +} + +var gen = yieldAndReturn() +console.log(gen.next()); // { value: "Y", done: false } +console.log(gen.next()); // { value: "R", done: true } +console.log(gen.next()); // { value: undefined, done: true } +</pre> + +<h3 id="Generator_as_an_object_property">Generator as an object property</h3> + +<pre class="brush: js notranslate">const someObj = { + *generator () { + yield 'a'; + yield 'b'; + } +} + +const gen = someObj.generator() + +console.log(gen.next()); // { value: 'a', done: false } +console.log(gen.next()); // { value: 'b', done: false } +console.log(gen.next()); // { value: undefined, done: true } +</pre> + +<h3 id="Generator_as_an_object_method">Generator as an object method</h3> + +<pre class="brush: js notranslate">class Foo { + *generator () { + yield 1; + yield 2; + yield 3; + } +} + +const f = new Foo (); +const gen = f.generator(); + +console.log(gen.next()); // { value: 1, done: false } +console.log(gen.next()); // { value: 2, done: false } +console.log(gen.next()); // { value: 3, done: false } +console.log(gen.next()); // { value: undefined, done: true } +</pre> + +<h3 id="Generator_as_a_computed_property">Generator as a computed property</h3> + +<pre class="brush: js notranslate">class Foo { + *[Symbol.iterator] () { + yield 1; + yield 2; + } +} + +const SomeObj = { + *[Symbol.iterator] () { + yield 'a'; + yield 'b'; + } +} + +console.log(Array.from(new Foo)); // [ 1, 2 ] +console.log(Array.from(SomeObj)); // [ 'a', 'b' ] +</pre> + +<h3 id="Generators_are_not_constructable">Generators are not constructable</h3> + +<pre class="brush: js notranslate">function* f() {} +var obj = new f; // throws "TypeError: f is not a constructor +</pre> + +<h3 id="Generator_defined_in_an_expression">Generator defined in an expression</h3> + +<pre class="brush: js notranslate">const foo = function* () { + yield 10; + yield 20; +}; + +const bar = foo(); +console.log(bar.next()); // {value: 10, done: false}</pre> + +<h3 id="Generator_example">Generator example</h3> + +<pre class="syntaxbox notranslate">function* powers(n){ + //endless loop to generate + for(let current =n;; current *= n){ + yield current; + } +} + +for(let power of powers(2)){ + //controlling generator + if(power > 32) break; + console.log(power) + //2 + //4 + //8 + //16 + //32 +}</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-generator-function-definitions', 'function*')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("javascript.statements.generator_function")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Operators/function*", "function* expression")}}</li> + <li>{{jsxref("GeneratorFunction")}} object</li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a></li> + <li>{{jsxref("Operators/yield", "yield")}}</li> + <li>{{jsxref("Operators/yield*", "yield*")}}</li> + <li>{{jsxref("Function")}} object</li> + <li>{{jsxref("Statements/function", "function declaration")}}</li> + <li>{{jsxref("Operators/function", "function expression")}}</li> + <li>{{jsxref("Functions_and_function_scope", "Functions and function scope")}}</li> + <li>Other web resources: + <ul> + <li><a href="http://facebook.github.io/regenerator/">Regenerator</a> an ES2015 generator compiler to ES5</li> + <li><a href="http://www.youtube.com/watch?v=qbKWsbJ76-s">Forbes Lindesay: Promises and Generators: control flow utopia -- JSConf EU 2013</a></li> + <li><a href="https://github.com/mozilla/task.js">Task.js</a></li> + <li><a href="https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/async%20%26%20performance/ch4.md#iterating-generators-asynchronously">Iterating generators asynchronously</a></li> + </ul> + </li> +</ul> diff --git a/files/my/web/javascript/reference/statements/index.html b/files/my/web/javascript/reference/statements/index.html new file mode 100644 index 0000000000..b5f4f3d5e0 --- /dev/null +++ b/files/my/web/javascript/reference/statements/index.html @@ -0,0 +1,130 @@ +--- +title: Statements and declarations +slug: Web/JavaScript/Reference/Statements +tags: + - JavaScript + - Landing page + - NeedsTranslation + - Reference + - TopicStub + - statements +translation_of: Web/JavaScript/Reference/Statements +--- +<div>{{jsSidebar("Statements")}}</div> + +<p>JavaScript applications consist of statements with an appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon. This isn't a keyword, but a group of keywords.</p> + +<h2 id="Statements_and_declarations_by_category">Statements and declarations by category</h2> + +<p>For an alphabetical listing see the sidebar on the left.</p> + +<h3 id="Control_flow">Control flow</h3> + +<dl> + <dt>{{jsxref("Statements/block", "Block")}}</dt> + <dd>A block statement is used to group zero or more statements. The block is delimited by a pair of curly brackets.</dd> + <dt>{{jsxref("Statements/break", "break")}}</dt> + <dd>Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.</dd> + <dt>{{jsxref("Statements/continue", "continue")}}</dt> + <dd>Terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.</dd> + <dt>{{jsxref("Statements/Empty", "Empty")}}</dt> + <dd>An empty statement is used to provide no statement, although the JavaScript syntax would expect one.</dd> + <dt>{{jsxref("Statements/if...else", "if...else")}}</dt> + <dd>Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.</dd> + <dt>{{jsxref("Statements/switch", "switch")}}</dt> + <dd>Evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.</dd> + <dt>{{jsxref("Statements/throw", "throw")}}</dt> + <dd>Throws a user-defined exception.</dd> + <dt>{{jsxref("Statements/try...catch", "try...catch")}}</dt> + <dd>Marks a block of statements to try, and specifies a response, should an exception be thrown.</dd> +</dl> + +<h3 id="Declarations">Declarations</h3> + +<dl> + <dt>{{jsxref("Statements/var", "var")}}</dt> + <dd>Declares a variable, optionally initializing it to a value.</dd> + <dt>{{jsxref("Statements/let", "let")}}</dt> + <dd>Declares a block scope local variable, optionally initializing it to a value.</dd> + <dt>{{jsxref("Statements/const", "const")}}</dt> + <dd>Declares a read-only named constant.</dd> +</dl> + +<h3 id="Functions_and_classes">Functions and classes</h3> + +<dl> + <dt>{{jsxref("Statements/function", "function")}}</dt> + <dd>Declares a function with the specified parameters.</dd> + <dt>{{jsxref("Statements/function*", "function*")}}</dt> + <dd>Generator Functions enable writing <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">iterators</a> more easily.</dd> + <dt>{{jsxref("Statements/async_function", "async function")}}</dt> + <dd>Declares an async function with the specified parameters.</dd> + <dt>{{jsxref("Statements/return", "return")}}</dt> + <dd>Specifies the value to be returned by a function.</dd> + <dt>{{jsxref("Statements/class", "class")}}</dt> + <dd>Declares a class.</dd> +</dl> + +<h3 id="Iterations">Iterations</h3> + +<dl> + <dt>{{jsxref("Statements/do...while", "do...while")}}</dt> + <dd>Creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.</dd> + <dt>{{jsxref("Statements/for", "for")}}</dt> + <dd>Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop.</dd> + <dt>{{jsxref("Statements/for_each...in", "for each...in")}} </dt> + <dd>Iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.</dd> + <dt>{{jsxref("Statements/for...in", "for...in")}}</dt> + <dd>Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.</dd> + <dt>{{jsxref("Statements/for...of", "for...of")}}</dt> + <dd>Iterates over iterable objects (including {{jsxref("Global_Objects/Array","arrays","","true")}}, array-like objects, <a href="/en-US/docs/JavaScript/Guide/Iterators_and_Generators">iterators and generators</a>), invoking a custom iteration hook with statements to be executed for the value of each distinct property.</dd> + <dt>{{jsxref("Statements/for-await...of", "for await...of")}}</dt> + <dd>Iterates over async iterable objects, array-like objects, <a href="/en-US/docs/JavaScript/Guide/Iterators_and_Generators">iterators and generators</a>, invoking a custom iteration hook with statements to be executed for the value of each distinct property.</dd> + <dt>{{jsxref("Statements/while", "while")}}</dt> + <dd>Creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.</dd> +</dl> + +<h3 id="Others">Others</h3> + +<dl> + <dt>{{jsxref("Statements/debugger", "debugger")}}</dt> + <dd>Invokes any available debugging functionality. If no debugging functionality is available, this statement has no effect.</dd> + <dt>{{jsxref("Statements/export", "export")}}</dt> + <dd>Used to export functions to make them available for imports in external modules, and other scripts.</dd> + <dt>{{jsxref("Statements/import", "import")}}</dt> + <dd>Used to import functions exported from an external module, another script.</dd> + <dt><a href="/en-US/docs/Web/JavaScript/Reference/Statements/import.meta"><code>import.meta</code></a></dt> + <dd>An object exposing context-specific metadata to a JavaScript module.</dd> + <dt>{{jsxref("Statements/label", "label")}}</dt> + <dd>Provides a statement with an identifier that you can refer to using a <code>break</code> or <code>continue</code> statement.</dd> +</dl> + +<dl> + <dt>{{jsxref("Statements/with", "with")}} </dt> + <dd>Extends the scope chain for a statement.</dd> +</dl> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-ecmascript-language-statements-and-declarations', 'ECMAScript Language: Statements and Declarations')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.statements")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators">Operators</a></li> +</ul> |