mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-05 14:27:51 +02:00
[javascript pt-br] : Funções, Escopos e Closures
This commit is contained in:
@@ -209,7 +209,7 @@ if (count == 3){
|
|||||||
// executa se count não é 3 nem 4
|
// executa se count não é 3 nem 4
|
||||||
}
|
}
|
||||||
|
|
||||||
// Como se faz `while`.
|
// Como se faz um `while`.
|
||||||
while (true){
|
while (true){
|
||||||
// Um loop infinito!
|
// Um loop infinito!
|
||||||
}
|
}
|
||||||
@@ -224,7 +224,7 @@ do {
|
|||||||
// initialisation; continue condition; iteration.
|
// initialisation; continue condition; iteration.
|
||||||
|
|
||||||
// O loop `for` é o mesmo de C e Java:
|
// O loop `for` é o mesmo de C e Java:
|
||||||
// inicialização, condição de continuar; iteração
|
// inicialização, condição para continuar; iteração
|
||||||
for (var i = 0; i < 5; i++){
|
for (var i = 0; i < 5; i++){
|
||||||
// vai rodar cinco vezes
|
// vai rodar cinco vezes
|
||||||
}
|
}
|
||||||
@@ -260,79 +260,84 @@ switch (grade) {
|
|||||||
|
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
// 4. Functions, Scope and Closures
|
// 4. Funções, Escopos e Closures
|
||||||
|
|
||||||
// JavaScript functions are declared with the `function` keyword.
|
// Funções Javascript são declaradas com a palavra-chave `function`.
|
||||||
function myFunction(thing){
|
function myFunction(thing){
|
||||||
return thing.toUpperCase();
|
return thing.toUpperCase();
|
||||||
}
|
}
|
||||||
myFunction("foo"); // = "FOO"
|
myFunction("foo"); // = "FOO"
|
||||||
|
|
||||||
// Note that the value to be returned must start on the same line as the
|
// Repare que o valor a ser retornado deve começar na mesma linha que
|
||||||
// `return` keyword, otherwise you'll always return `undefined` due to
|
// a palavra-chave `return`, senão você sempre irá retornar `undefined`
|
||||||
// automatic semicolon insertion. Watch out for this when using Allman style.
|
// visto que o ponto-e-vírgula é inserido automáticamente nas quebras de
|
||||||
|
// linha. Preste atenção quando usar o estilo Allman.
|
||||||
function myFunction()
|
function myFunction()
|
||||||
{
|
{
|
||||||
return // <- semicolon automatically inserted here
|
return // <- ponto-e-vírgula adicionado automaticamente aqui
|
||||||
{
|
{
|
||||||
thisIsAn: 'object literal'
|
thisIsAn: 'object literal'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
myFunction(); // = undefined
|
myFunction(); // = undefined
|
||||||
|
|
||||||
// JavaScript functions are first class objects, so they can be reassigned to
|
// Funções Javascript são objetos de primeira classe, portanto elas podem
|
||||||
// different variable names and passed to other functions as arguments - for
|
// ser atribuídas a nomes de variáveis e serem passadas para outras funções
|
||||||
// example, when supplying an event handler:
|
// como argumentos - por exemplo, quando criamos um manipulador de eventos:
|
||||||
function myFunction(){
|
function myFunction(){
|
||||||
// this code will be called in 5 seconds' time
|
// este código será chamado em 5 segundos
|
||||||
}
|
}
|
||||||
setTimeout(myFunction, 5000);
|
setTimeout(myFunction, 5000);
|
||||||
// Note: setTimeout isn't part of the JS language, but is provided by browsers
|
// Nota: `setTimeout` não é parte da linguagem Javascript, mas é provido pelos
|
||||||
// and Node.js.
|
// browsers e o Node.js.
|
||||||
|
|
||||||
// Function objects don't even have to be declared with a name - you can write
|
// Objetos de funções não precisam nem serem declarados com nome - você pode
|
||||||
// an anonymous function definition directly into the arguments of another.
|
// escrever a definição de uma função anônima diretamente nos argumentos de
|
||||||
|
// outra função.
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
// this code will be called in 5 seconds' time
|
// este código será chamado em 5 segundos
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
// JavaScript has function scope; functions get their own scope but other blocks
|
// O Javascript tem escopo de função; as funções tem seu próprio escopo,
|
||||||
// do not.
|
// mas outros blocos não.
|
||||||
if (true){
|
if (true){
|
||||||
var i = 5;
|
var i = 5;
|
||||||
}
|
}
|
||||||
i; // = 5 - not undefined as you'd expect in a block-scoped language
|
i; // = 5 - não `undefined` como você esperaria numa linguagem de blogo-escopo
|
||||||
|
|
||||||
// This has led to a common pattern of "immediately-executing anonymous
|
// Isso levou a padrão comum chamado de IIFE (Imediately Invoked Function
|
||||||
// functions", which prevent temporary variables from leaking into the global
|
// Expression) ou (Expressão de Função Invocada Imediatamente), que previne
|
||||||
// scope.
|
// que variáveis temporárias vazem para o escopo global.
|
||||||
(function(){
|
(function(){
|
||||||
var temporary = 5;
|
var temporary = 5;
|
||||||
// We can access the global scope by assiging to the "global object", which
|
// Nós podemos acessar o escopo global definindo o "objeto global", que
|
||||||
// in a web browser is always `window`. The global object may have a
|
// no browser vai ser sempre `window`. O objeto global pode ter um nome
|
||||||
// different name in non-browser environments such as Node.js.
|
// diferente para ambiente não-browser como o Node.js.
|
||||||
window.permanent = 10;
|
window.permanent = 10;
|
||||||
})();
|
})();
|
||||||
temporary; // raises ReferenceError
|
temporary; // levanta um erro de referência inexiste
|
||||||
permanent; // = 10
|
permanent; // = 10
|
||||||
|
|
||||||
// One of JavaScript's most powerful features is closures. If a function is
|
// Uma das principais características do Javascript é a closure. Que é
|
||||||
// defined inside another function, the inner function has access to all the
|
// uma função definida dentro de outra função, a função interna pode acessar
|
||||||
// outer function's variables, even after the outer function exits.
|
// todas as variáveis da função externa, mesmo depois da função de fora
|
||||||
|
// finalizar sua execução.
|
||||||
function sayHelloInFiveSeconds(name){
|
function sayHelloInFiveSeconds(name){
|
||||||
var prompt = "Hello, " + name + "!";
|
var prompt = "Hello, " + name + "!";
|
||||||
// Inner functions are put in the local scope by default, as if they were
|
|
||||||
// declared with `var`.
|
// Funções internas são colocadas no escopo local por padrão, assim como
|
||||||
|
// se fossem declaradas com `var`.
|
||||||
function inner(){
|
function inner(){
|
||||||
alert(prompt);
|
alert(prompt);
|
||||||
}
|
}
|
||||||
setTimeout(inner, 5000);
|
setTimeout(inner, 5000);
|
||||||
// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will
|
// `setTimeout` é assíncrono, portanto a função `sayHelloInFiveSeconds`
|
||||||
// exit immediately, and setTimeout will call inner afterwards. However,
|
// vai sair imediatamente, e o `setTimeout` irá chamar a interna depois.
|
||||||
// because inner is "closed over" sayHelloInFiveSeconds, inner still has
|
// Entretanto. como a interna é fechada dentro de "sayHelloInFiveSeconds",
|
||||||
// access to the `prompt` variable when it is finally called.
|
// a interna permanece podendo acessar a variável `prompt` quando depois
|
||||||
|
// de chamada.
|
||||||
}
|
}
|
||||||
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
|
sayHelloInFiveSeconds("Adam"); // Vai abrir um popup com "Hello, Adam!" em 5s
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
// 5. More about Objects; Constructors and Prototypes
|
// 5. More about Objects; Constructors and Prototypes
|
||||||
|
Reference in New Issue
Block a user