2

I've just started working with javascript and I saw something which I can't explain.
I have js file named TestClass.js and its the following:

TestClass.SetError = function (errorDescription, errorCode, typedErrorCode)
{
    alert("SetError ");
}  

function SetError2(errorCode)
{
    alert("SetError2 ");
}  

Can someone please explain me the difference between the SetError and SetError2?

Cœur
  • 37,241
  • 25
  • 195
  • 267
liorafar
  • 2,264
  • 4
  • 19
  • 39
  • 1
    possible duplicate of [What is the difference between a function expression vs declaration in Javascript?](http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip) – deceze Sep 11 '12 at 10:02
  • Another related question: http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname – Dmitry Pashkevich Sep 11 '12 at 10:07

6 Answers6

1

SetError is a method of the object TestClass. Whereas SetError2 is a global function.

1

The first piece of code (SetError) contains Anonymous Function Expression, where the second piece of code (SetError2) cotains a Function Declaration. See Function Declarations vs. Function Expressions

Another good article: Named function expressions demystified

Excerpt:

function declarations are parsed and evaluated before any other expressions are. Even if declaration is positioned last in a source, it will be evaluated foremost any other expressions contained in a scope.

That's basically the main difference. It's not huge but worth being aware of.

Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73
1

the difference lies in how the browser loads them into the execution context.

function loads before any code is executed.

While expressions loads only when the interpreter reaches that line of code.

AboQutiesh
  • 1,696
  • 2
  • 9
  • 14
0

SetError2 is a "named" method. TestClass.SetError is a member variable of TestClass that references an anonymous method. Which means later you can do TestClass.SetError = function(){ alert ("SetError2");}

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
0

Javascript has Objects and Functions.

An Object can contains Objects and/or functions.

this is an object:

var TestClass = {};

you can add a new object to TestClass Object as following:

var TestClass = { object : {}};

Then you can have an access to it like this:

var a = TestClass.object;

You can have a function in an object:

var TestClass = {object:{}, setError:function(){/*function body*/}};

Now you can invoke the function of test class like this:

TestClass.setError();

In addition, Function itself can be defined like this:

function setError2(){/*function body*/}

and you may invoke like this:

setError2();

usually, we define functions in objects to somehow implement OO in Javascript and prevent messy js codes.

Reza Owliaei
  • 3,293
  • 7
  • 35
  • 55
-2

This:

function SetError2(errorCode) {
  alert("SetError2 ");
}

Is equivalent to this:

window.SetError2 = function(errorCode) {
  alert("SetError2 ");
}
troelskn
  • 115,121
  • 27
  • 131
  • 155
  • Not exactly... the first one is function declaration and the second one is function expression. See http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname – Dmitry Pashkevich Sep 11 '12 at 10:07
  • @DmitryPashkevich I meant conceptually. Try this: `function foo() {alert("foo"); } ; window.foo();`. I realise there's a subtle difference in how they are handled internally by the JS runtime, but for the OP to understand the difference between his two cases, I figured it might help to understand. – troelskn Sep 11 '12 at 22:06
  • Oh, now I see... But since the difference is really sublte and the OP is asking a direct question about it, I'd give him maximum information so he realizes it. – Dmitry Pashkevich Sep 12 '12 at 07:40
  • I think you're right. I likely misunderstood the question. I thought he meant what the code did, but he may be asking why there's a difference in syntax to apparently achieve the same thing. A bit unclear from the question actually. – troelskn Sep 12 '12 at 07:58
  • I think that this answer is the best to explain why the function was not called as expected. And so many downvotes... how silly. –  Oct 01 '12 at 14:13