diff options
Diffstat (limited to 'files/pt-pt/web/javascript/reference/statements/block/index.html')
-rw-r--r-- | files/pt-pt/web/javascript/reference/statements/block/index.html | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/files/pt-pt/web/javascript/reference/statements/block/index.html b/files/pt-pt/web/javascript/reference/statements/block/index.html new file mode 100644 index 0000000000..4a6507702f --- /dev/null +++ b/files/pt-pt/web/javascript/reference/statements/block/index.html @@ -0,0 +1,117 @@ +--- +title: Bloco (block) +slug: Web/JavaScript/Reference/Statements/block +tags: + - Declaração + - Funcionalidade de Linguagem + - JavaScript + - Referencia +translation_of: Web/JavaScript/Reference/Statements/block +original_slug: Web/JavaScript/Reference/Extratos_e_declarações/bloco +--- +<div>Bloco {{jsSidebar("Statements")}}</div> + +<p>Uma <strong>declaralção bloco </strong>(ou <strong>declaração composto</strong> em outras linguagens) é utilizada para agrupar zero ou mais declarações. O bloco é delimitado por um par de chavetas (“chavetas { }”) e opcionalmente poderá ser {{jsxref("Statements/label", "labelled", "", 1)}}:</p> + +<div>{{EmbedInteractiveExample("pages/js/statement-block.html", "taller")}}</div> + + + +<h2 id="Sintaxe">Sintaxe</h2> + +<h3 id="Declaração_de_Bloco">Declaração de Bloco</h3> + +<pre class="syntaxbox">{ + <em>StatementList</em> +} +</pre> + +<h3 id="Declaração_de_Bloco_Etiquetado">Declaração de Bloco Etiquetado</h3> + +<pre class="syntaxbox"><em>LabelIdentifier</em>: { + <em>StatementList</em> +} +</pre> + +<dl> + <dt><code>StatementList</code></dt> + <dd>Statements grouped within the block statement.</dd> + <dt><code>LabelIdentifier</code></dt> + <dd>An optional {{jsxref("Statements/label", "label", "", 1)}} for visual identification or as a target for {{jsxref("Statements/break", "break")}}.</dd> +</dl> + +<h2 id="Descrição">Descrição</h2> + +<p>The block statement is often called <strong>compound statement</strong> in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript. The opposite behavior is possible using an <a href="/en-US/docs/Web/JavaScript/Reference/Statements/Empty">empty statement</a>, where you provide no statement, although one is required.</p> + +<p>Blocks are commonly used in association with {{jsxref("Statements/if...else", "if...else")}} and {{jsxref("Statements/for", "for")}} statements.</p> + +<h3 id="Block_Scoping_Rules">Block Scoping Rules</h3> + +<h4 id="With_var_or_function_declaration_in_non-strict_mode">With <code>var</code> or function declaration in non-strict mode</h4> + +<p>Variables declared with <code>var</code> or created by <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function declarations</a> in non-strict mode <strong>do not</strong> have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. For example:</p> + +<pre class="brush: js example-bad">var x = 1; +{ + var x = 2; +} +console.log(x); // logs 2 +</pre> + +<p>This logs 2 because the <code>var x</code> statement within the block is in the same scope as the <code>var x</code> statement before the block.</p> + +<p>In non-strict code, function declarations inside blocks behave strangely. Do not use them.</p> + +<h4 id="With_let_const_or_function_declaration_in_strict_mode">With <code>let</code>, <code>const</code> or function declaration in strict mode</h4> + +<p>By contrast, identifiers declared with {{jsxref("Statements/let", "let")}} and {{jsxref("Statements/const", "const")}} <strong>do have </strong>block scope:</p> + +<pre class="brush: js">let x = 1; +{ + let x = 2; +} +console.log(x); // logs 1</pre> + +<p>The <code>x = 2</code> is limited in scope to the block in which it was defined.</p> + +<p>The same is true of <code>const</code>:</p> + +<pre class="brush: js">const c = 1; +{ + const c = 2; +} +console.log(c); // logs 1 and does not throw SyntaxError...</pre> + +<p>Note that the block-scoped <code>const c = 2</code> <em>does not</em> throw a <code>SyntaxError: Identifier 'c' has already been declared</code> because it can be declared uniquely within the block.</p> + +<p>In <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>, starting with ES2015, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode.</p> + +<h2 id="Especificações">Especificações</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Especificação</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-block', 'Block statement')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilidade_de_navegador">Compatibilidade de navegador</h2> + + + +<p>{{Compat("javascript.statements.block")}}</p> + +<h2 id="Consulte_também">Consulte também</h2> + +<ul> + <li>{{jsxref("Statements/while", "while")}}</li> + <li>{{jsxref("Statements/if...else", "if...else")}}</li> + <li>{{jsxref("Statements/let", "let")}}</li> +</ul> |