2

Why does the following work:

function outsideFunction() {
   return {
    insideFunction: insideFunction
}

function insideFunction() {
    ...stuff
}
}

isn't

function insideFunction() {
    ...stuff
}

the same as

var insideFunction = function() { ...stuff }

which would cause var insideFunction to be hoisted to the top? Shouldn't insideFunction return undefined when used in the object declaration?

Can I do the same thing with an object? In other words, can I do:

return {objectName: objectName}
var objectName = {}
gariepy
  • 3,576
  • 6
  • 21
  • 34
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
  • 3
    `hoisting` is the answer – Tushar Jul 16 '15 at 13:12
  • possible duplicate of [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – SK. Jul 16 '15 at 13:13
  • @Tushar Hoisting is just that js moves all declaration to the top not the initializations. This has to do with the variable scope and that the functions don't use the variables until it's executet later in code... – bang Jul 16 '15 at 13:16
  • 1
    @bang `function` declaration *statements* are also hoisted to the top, so that's why binding a function to a local symbol via a `var` initialization is *not* the same as doing so with a function declaration statement. – Pointy Jul 16 '15 at 13:18
  • @Tushar I addressed hoisting in my original post. Hoisting just moves var insideFunction to the top. However, as I understood it, before the variable is initialized it should resolve to undefined... – pQuestions123 Jul 16 '15 at 13:19
  • @DavidHaim tried what? I have tried doing this with functions (i.e. using them in an object initialization before declaring them and it works. I have not tried it with objects. – pQuestions123 Jul 16 '15 at 13:20
  • If the initialisation is not hoisted as well as the declaration then it can still work if the call is not made until after it has been initialised. ie outsideFunction is called later. – QuentinUK Jul 16 '15 at 13:20
  • 1
    Sorry, to quick... Hoisting is the answer, and also the answer to why the last block of code won't work. It's only the declaration that's moved to the top.... – bang Jul 16 '15 at 13:25

2 Answers2

5

This is because of hoisting - things are accessible before you've declared them, as all declaration is "hoisted" to the top of your scope. You say:

isn't

function insideFunction() {
    ...stuff
}

the same as

var insideFunction = function() { ...stuff }

No. As per the MDN documentation:

Function declarations in JavaScript are hoisting the function definition. You can use the function before you declared it:

hoisted(); // logs "foo"

function hoisted() {
  console.log("foo");
}

Note that function expressions are not hoisted:

notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
   console.log("bar");
};

So with your final question:

can I do:

return {objectName: objectName}
var objectName = {}

Again, no. Whilst objectName will be declared (so you avoid a Reference Error), it's value at the point you return will be undefined.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
0

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). And JavaScript initializations are not hoisted. So the answer to your question is no. For more information go to the follwoing link w3schools.com

jafaritaqi
  • 578
  • 7
  • 14