Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does Chrome manages references to javascript constructor

Please consider this code :

function A() {
    console.log("first");
}

var f = A;

function A() {
    console.log("second");
}

var g = A;

f();
g();

It outputs "first", "second" in firebug, which is what I thought it should do.
But it outputs "second", "second", in Chrome's console, or in firefox (when executing from a file, not in firebug).
Why should the reference kept in 'f' be changed I do the second "function A() {" ??

It seems like hoisting is the problem (see apsillers' answer bellow). But then, why does this example work correctly (I mean output first-second) :

var A = function A() {
    console.log("first");
}

var f = A;

A = function A() {
    console.log("second");
}

var g = A;

f();
g();

The fact that I used "A = ..." in the second function declaration blocks the hoisting of this function ?

like image 487
Sebastien Avatar asked Aug 01 '26 13:08

Sebastien


1 Answers

Function declarations are hoisted to the top of their scope, so your code is interpreted like so:

function A() {
    console.log("first");
}

// overwrite previous A
function A() {
    console.log("second");
}

// variable declarations are hoisted as well
// (not directly related to your problem here, but worth noting)
var f;
var g;

f = A;
g = A;

f();
g();

which produces the output of second, second in any modern browser.

In your second example, with var A = ..., you're now using function expressions, rather than function declarations. Function expressions are not hoisted.

Firebug weirdness

It appears that -- for some reason -- Firebug doesn't correctly perform function declaration hoisting:

console.log(bar);  // this line incorrectly produces a ReferenceError!

function bar() { }

This code snippet should log function bar() { }. It does so in any proper browser environment, but not Firebug.

EDIT:

The reason for the Firebug behavior is that Firebug code runs inside of a block, and function declarations are not valid in blocks. However, browsers will still handle them in non-strict mode, but how they handle them differs. Firefox treats them as unhoisted (while IE and Chrome do hoist them, as it happens).

like image 153
apsillers Avatar answered Aug 04 '26 02:08

apsillers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!