Aprovecha el precio especial

Antes:$249

Currency
$209

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Comienza ahora

Termina en:

03d

13h

17m

55s

4

Hoisting

Para entender un poco del motor de Javascript.

En la mayoría de los lenguajes de programación, el ámbito / alcance de una variable está en el bloque donde fue definida (block-level scope), por ejemplo, dentro de un bloque de instrucciones if, for, while. En JavaScript (ECMAScript 5.1) esto no es así, puesto que el alcance de una variable es el de la función en la cual fue declarada (function-level scope).

El motor V8 de javascript consta de dos procesos Heap y Stack, cuando arranca la ejecucion de javascript el heap hace un barrido por todo nuestro codigo mientras que esl stack es el encargado de ejecutarlo.
En la etapa de Heap extiste algo que se llama efecto hoisting o “elevación” es una de las particularidades de JavaScript y se da cuando definimos una variable en el interior de una función (en el scope), entonces el intérprete interno pasa a declararla al comienzo de su contexto (la eleva)
(function () { for(var x = 0; x < 3; x += 1) { console.log(x); } console.log(x, "todavía existe!!"); }());
Así es como lo interpreta el motor
(function () { var x; // <- la variable es elevada for(x = 0; x < 3; x += 1) { console.log(x); } console.log(x, "todavía existe!!"); }());
Aca vemos otro ejemplo
var x = 5; (function () { console.log("x:", x); //se espera 5, pero retorna undefined var x = 10; console.log("x:", x); //se espera 10 }());
Como lo vio V8
var x = 5; (function () { var x; // <- la variable es elevada console.log("x:", x); //-> undefined x = 10; console.log("x:", x); //-> 10 }());
Hoisting no solamente actúa con las definición de variables también lo hacer con funciones, pero hay que prestar mucha atención en como están definidas las funciones.
console.log("fn1", fn1()); //que imprime? console.log("fn2", fn2()); //que imprime? //function expression var fn1 = function() { return 1; }; //function declaration function fn2() { return 2; }
Es muy posible que nuestra lógica apunte a pensar que V8 interprete de la siguiente forma.
var fn1 = function() { return 1; }; function fn2() { return 2; } console.log("fn1", fn1()); //que imprime? console.log("fn2", fn2()); //que imprime? //function expression //function declaration

En este caso no tenemos una definicion de fn1 lo que tenemos es una asignacion por referencia de una funcion a una variable, por lo que V8 lo interpreta de la siguiente forma.
var fn1; // <- la variable es elevada function fn2() { return 2; } // <- declaración y definición son elevadas console.log("fn1", fn1()); // throw TypeError: undefined is not a function console.log("fn2", fn2()); //2 //function expression fn1 = function() { return 1; };
Por definición Hoisting hace que la declaración de variables sea procesada antes de que cualquier código se ejecute, declarar una variable en cualquier parte es lo mismo que declararla al inicio. Esto también significa que una variable puede ser usada antes de su declaración.

Espero que les sea de utilidad.
Saludos

Escribe tu comentario
+ 2
Ordenar por:
1
2 años

I’m grateful for this article’s simplicity and clarity. It’s refreshing to read something that gets straight to the point without any unnecessary jargon. Women’s color blindness highlights the importance of universal design principles. By creating environments, products, and visual information that are accessible and inclusive for individuals with color vision deficiencies, we ensure equal participation and opportunity for everyone. Get detailed information about Women’s Color Blindness.

1
2 años

Concept of internet is changing from a place to learn, to a place to live. A lot of us are using our phones to browse internet. We learn, buy and even love online. A chance for improvement and making money in this era is a shot you don’t want to miss. Mehr news recently published an article about digital marketing that can change the way you look at thing. Check it out.

1
2 años

I read your full article and I like it very much. One Of The Best Blogs I Found. It will also feel considerably better than 1000Hz with a polling rate of 500Hz. Compared to 500Hz, the 1000Hz polling rate captures substantially more information regarding mouse movement. Therefore, you should switch to 500Hz for a better feel if you don’t like the floating feeling of 1000Hz. Read the article to learn about Mouse Polling Rate For Gaming.
Visit - https://hub.docker.com/r/mouseratechecker/best-mouse-polling-rate-for-gaming

1
8Puntos
3 años

have found your post on this forum, I hope you will update your post. Your article is very effective Company formation

1
8Puntos
3 años

You may have heard that JavaScript code can run faster on the browser if it’s written with a few extra rules in mind. But what are these rules, and how do they work? In this article, we’ll talk about the “hoisting” concept in JavaScript and show how it can improve the performance of your scripts. Mega Test Bank

1
3 años

Thank you for sharing the above information. Keep it up. When there is Stuttering issue In Games, the game feels slow, appears laggy, and skips frames. This can result in a delay in player actions, which disrupts gameplay. If you facing a problem like this then read this article to solve the stuttering issue in games.

1
7775Puntos
3 años

Me parece que se rompió el formato del post?

1
2Puntos
4 años

I have found your post on this forum, I hope you will update your post. Your article is very effective Stickman Hook

1
7Puntos
5 años

Can nicely write on similar topics! Welcome to here you’ll find out how it should look. soap2day

1
3Puntos
6 años

The post has helped me understand more about hoisting. It’s easy to understand for beginners.
spanish dictionary

1
3Puntos
5 años

If you want to sell your used old car in hamburg then auto ankauf hamburg is best choice for selling out your old cars

1
124Puntos
5 años

An article worth noting and should be learned. Thank you for providing this great information. shell shockers

1
5 años

When participating in the forum, I found very good and very positive information from the forum. I will stay tuned for the latest news from the forum.
geometry dash