1

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

What's the difference in declaration of this functions i know example one is the normal way to do it, why we need two and three?

function one(var1,var2) {
   alert("inside functtion one");
}

two = function (var1,var2) {
   alert("inside function two");
}

var three = function (var1,var2) {
   alert("inside function three");
}
Community
  • 1
  • 1
JohnA
  • 1,875
  • 8
  • 28
  • 34
  • 2
    three and one are (for this example) identical.. two messes with the scope?? attaches it to the window object iirc? – rlemon May 16 '12 at 20:01
  • Check out: http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/ – Ayman Safadi May 16 '12 at 20:02
  • @rlemon three and one are subtly different in that you can call one before its declaration, but you can't call three until after its assignment. Also one is a named function while three is anonymous, but you can work around that. – Neil May 16 '12 at 20:24
  • after three is declared does it not function the same as a named function... i.e.i can call it the same and there is no wonky IE memory issues. – rlemon May 16 '12 at 20:32
  • as per the first part of your comment... TBH I did not know that, good to know though :P – rlemon May 16 '12 at 20:33

1 Answers1

0

The first and the third are just two ways to declare a function which exists globally in the scope chain. The middle is attaching the function two to the window object and allowing it to exist there.

console.log(window.one); // undefined
console.log(window.two);
console.log(window.three); // undefined
rlemon
  • 17,518
  • 14
  • 92
  • 123
  • note: this is only **one** difference. depending on the context you are asking your question in closures have a lot to do with it. where are these declarations happening? – rlemon May 16 '12 at 20:06
  • its script run in header of page in header – JohnA May 17 '12 at 19:14
  • and that makes what difference? Not where in the document, where relative to the rest of the javascript code. is it in a closure? – rlemon May 17 '12 at 19:27