hoisting

Function hoisting in js

浪子不回头ぞ 提交于 2019-11-28 11:18:48
问题 function mymethod(){ alert("global mymethod"); } function mysecondmethod(){ alert("global mysecondmethod"); } function hoisting(){ alert(typeof mymethod); alert(typeof mysecondmethod); mymethod(); // local mymethod mysecondmethod(); // TypeError: undefined is not a function // mymethod AND the implementation get hoisted function mymethod(){ alert("local mymethod"); } // Only the variable mysecondmethod get's hoisted var mysecondmethod = function() { alert("local mysecondmethod"); }; }

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

南笙酒味 提交于 2019-11-27 19:08:53
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? Functions are a type of object which are a type of value . Values can be stored in variables (and properties, and passed as arguments

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

不羁岁月 提交于 2019-11-27 17:06:15
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> foo(); function foo() { return; } </script> Although the "foo" function is called before it is declared,

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank? [duplicate]

寵の児 提交于 2019-11-27 16:08:11
This question already has an answer here: Why do I get the value “result” for this closure? 3 answers As part of my learning JavaScript, I try to write code to demonstrate the concept I am learning; today I'm learning hoisted variables. Here is the code I wrote: console.log("A: My name is " + name); function happy() { console.log ("1: I am " + feeling); var feeling = "happy"; console.log ("2: I am " + feeling); } happy(); var name = "Jim"; console.log("B: My name is " + name); I expected the following results: A: My name is undefined 1: I am undefined 2: I am happy B: My name is Jim However,

Need to understand Javascript function hoisting example

两盒软妹~` 提交于 2019-11-27 15:40:35
问题 I read the concept of Javascript Hoisting.Its pretty confusing but I saw some examples and got the idea what hoisting actually does. So basically " Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function). " But I am not able to understand the below implementation : var is_android = true; if (is_android) { function foo() { alert('I am Android'); } } else { function foo() { alert('I am NOT

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

房东的猫 提交于 2019-11-27 15:23:23
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 second one(2); is called. This behavior is intuitive: the function inner is defined only if a==1 . But

How does hoisting work if JavaScript is an interpreted language?

╄→гoц情女王★ 提交于 2019-11-27 14:30:41
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 not declared) var bar = 111; console.log(bar); // 111 } Is implicitly understood as: function do

Why a variable defined global is undefined? [duplicate]

拟墨画扇 提交于 2019-11-27 04:25:16
This question already has an answer here: 'Hoisted' JavaScript Variables 3 answers 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 variable... And how I can fix this so I don't get a undefined from a global variable? You have just stumbled on a js "feature" called

Order of hoisting in JavaScript

南楼画角 提交于 2019-11-27 02:09:25
function g () { var x; function y () {}; var z; } I would like to know exactly what order the above code becomes when hoisted. Theory 1: Order between var s and function s remains as-is: function g () { var x; function y () {}; var z; } Theory 2: var s come before function s: function g () { var x; var z; function y () {}; } Theory 3: function s come before var s: function g () { function y () {}; var x; var z; } Which theory is correct? apsillers Functions are hoisted first, then variable declarations, per ECMAScript 5, section 10.5 which specifies how hoisting happens: We first have step 5

Why do catch clauses have their own lexical environment?

百般思念 提交于 2019-11-27 02:06:47
Consider the following excerpt from ECMA-262 v5.1 (which I recently saw in this question ): A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a WithStatement, or a Catch clause of a TryStatement and a new