hoisting

How many JavaScript programs are executed for a single web-page in the browser?

孤街浪徒 提交于 2019-11-26 22:30:57
问题 JavaScript programs consist of statements and function declarations. When a JavaScript program is executed, these two steps occur: the code is scanned for function declarations, and every func. declaration is "executed" (by creating a function object) and a named reference to that function is created (so that this function can be called from within a statement) the statements are executed (evaluated) sequentially (as they appear in the code) Because of that, this works just fine : <script>

Do inner functions in JavaScript get hoisted? How do the scoping rules apply to them?

限于喜欢 提交于 2019-11-26 18:30:12
问题 I thought that JavaScript doesn't have block scope, but function scope, and that declarations are hoisted from their block to the top of their parent functions. However, the following code does not work as expected: function one(a) { console.log("one called for " + a); if (a == 1) { function inner(b) { console.log("inner called for " + b); } inner(123); } inner(456); } one(1); one(2); one(3); The first one(1); call proceeds normally, without any errors, however the execution stops when the

'Hoisted' JavaScript Variables

坚强是说给别人听的谎言 提交于 2019-11-26 17:53:55
I do not fully understand why the following displays "hoisted" towards the end. var x = 'set'; var y = function () { // WHAT YOU DON'T SEE -> var x; // is effectively "hoisted" to this line! if (!x) { // You might expect the variable to be populated at this point...it is not // though, so this block executes var x = 'hoisted'; } alert(x); } //... and this call causes an alert to display "hoisted" y(); Any pointers would be appreciated. Quoting MDN Docs on var hoisting , Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable

Will const and let make the IIFE pattern unnecessary?

穿精又带淫゛_ 提交于 2019-11-26 17:52:49
As I understand it, the IIFE pattern is a work around to the fact that ES5 and below do not have a way to create block scopes. By wrapping everything in a function and immediately invoking it, we can create a scope. Now that let and const will gain support by more the browsers, does this reduce the need for something like the IIFE pattern? Bergi Yes, blocks are going to replace IEFEs, as soon as block-scoped declarations ( functions , let / const / class ) become widely adopted. You need a scope, e.g. for a closure? Here you have a block, be it a loop body or just part of a statement list.

Why does JavaScript hoist variables?

邮差的信 提交于 2019-11-26 17:30:54
Why does JavaScript hoist variables? What was the rationale of the designers when they decided to implement hoisting? Are there any other popular languages that do this? Please provide relevant links to documentation and/or records. lxgreen As Stoyan Stefanov explains in "JavaScript Patterns" book, the hoisting is result of JavaScript interpreter implementation. The JS code interpretation performed in two passes. During the first pass, the interpreter processes variable and function declarations. The second pass is the actual code execution step. The interpreter processes function expressions

How does hoisting work if JavaScript is an interpreted language?

我与影子孤独终老i 提交于 2019-11-26 16:46:04
问题 My understanding of an interpreter is that it executes program line by line and we can see the instant results, unlike compiled languages which convert code, then executes it. My question is, in Javascript, how does interpreter come to know that a variable is declared somewhere in the program and logs it as undefined ? Consider the program below: function do_something() { console.log(bar); // undefined (but in my understanding about an interpreter, it should be throwing error like variable

What happens when JavaScript variable name and function name is the same?

折月煮酒 提交于 2019-11-26 15:55:30
问题 I have the following code, where I declare a function and after it, a variable with the same name as the function: function a(x) { return x * 2; } var a; alert(a); I expected this to alert undefined , but if I run it, the alert will display the following: function a(x) { return x * 2 } If I assign a value to the variable (like var a = 4 ), the alert will display that value ( 4 ), but without this change a will be recognized as a function. Why is this happening? 回答1: Functions are a type of

Why a variable defined global is undefined? [duplicate]

最后都变了- 提交于 2019-11-26 11:09:40
问题 This question already has answers here : 'Hoisted' JavaScript Variables (3 answers) Closed 4 years ago . Hi guys here I have a simple function and a global variable. Why is myname undefined and not the string \"global\" ? var myname = \"global\"; // global variable function func() { alert(myname); // \"undefined\" var myname = \"local\"; alert(myname); // \"local\" } func(); Is not possible to refer to a outer variable that is define outside the scope of that function? and in this a global

JavaScript &#39;hoisting&#39; [duplicate]

久未见 提交于 2019-11-26 10:19:36
This question already has an answer here: Javascript function scoping and hoisting 15 answers I came across JavaScript 'hoisting' and I didn't figure out how this snippet of code really functions: var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); I know that function declaration like ( function a() {} ) is going to be hoisted to the top of the function b scope but it should not override the value of a (because function declarations override variable declarations but not variable initialization) so I expected that the value of the alert would be 10 instead of 1!! The

Are ES6 module imports hoisted?

北慕城南 提交于 2019-11-26 09:10:24
问题 I know that in the new ES6 module syntax, the JavaScript engine will not have to evaluate the code to know about all the imports/exports, it will only parse it and “know” what to load. This sounds like hoisting. Are the ES6 modules hoisted? And if so, will they all be loaded before running the code? Is this code possible? import myFunc1 from \'externalModule1\'; myFunc2(); if (Math.random()>0.5) { import myFunc2 from \'externalModule2\'; } 回答1: It will be a SyntaxError . According to this