5

I have tried folllowing two ways of referring a function:

First

let a = function() {
   somefunction();
}

Second

let a = somefunction;

Where somefunction is the following in both cases:

function somefunction() {
  alert("hello");
}

Is there any difference between these two ways?

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
  • As shown, there's little difference (apart from the redundant intermediate function in the first case). However, from the way your `somefunction` is defined it looks like it's only the part of the story; why don't you show the real code here? – raina77ow Oct 27 '13 at 21:43
  • 1
    is `:` in `somefunction :function` a typo? You meant to write `=`? – Roko C. Buljan Oct 27 '13 at 21:56
  • 1
    May not be a typo. Could be an object literal. – slebetman Oct 27 '13 at 22:06
  • 1
    @slebetman ok than explain `a=somefunction;` if an Object Literal was ment to be used – Roko C. Buljan Oct 27 '13 at 22:19
  • `var foo = {somefunction:function(){}};a=foo.somefunction` <-- he's probably just copying the js code from an object literal – slebetman Oct 27 '13 at 23:38
  • One concrete example is when people try to do this: `var get = document.getElementById`. Then when they try to call `get(id)` they're surprised it doesn't work – slebetman Oct 27 '13 at 23:39

5 Answers5

3

Yes, there is a difference between your two examples.

In the first case, you are defining a new anonymous (unnamed) function which calls somefunction. You are then assigning your new function definition to the variable a. a holds a reference to your new function.

In the second case, you are simply assigning your original function of somefunction to the variable a. The variable a then holds a reference to somefunction. You are not creating a new function as you are in the first case.

DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
  • And do these differ in functionality? ) – raina77ow Oct 27 '13 at 21:45
  • @raina77ow no, they do not, however they are different. The first is redundant and wastes memory. – DigitalZebra Oct 27 '13 at 21:46
  • To that I agree completely. The question is, should 'waste of memory' in this particular case be of so much trouble that it deserves to eventually morph into a SO question. ) Then again, the ways of OPs are puzzling sometimes. – raina77ow Oct 27 '13 at 21:48
  • 1
    Actually it differs in functionality as they might run in different scopes if you wrap your function inside an object, for example. Take a look at this example: http://jsfiddle.net/ARw3A/ – Guilherme Sehn Oct 27 '13 at 21:52
  • @GuilhermeSehn Now I'm really curious why that is... Why would the act of calling `myFunction` from within another function use the object scope (which is not accessible from `a`)? – DigitalZebra Oct 27 '13 at 22:13
  • I don't know why the language designers chose to make JavaScript behave like this, but that's how it works. :P – Guilherme Sehn Oct 27 '13 at 22:26
  • @GuilhermeSehn Take a look at the answer to this question: http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal I guess I never realized the difference between invoking a function as a method and as a function when it comes to object literals. – DigitalZebra Oct 27 '13 at 22:29
  • 1
    That clarified some things to me too. Stack Overflow is a very interesting community. You can share your knowledge and learn more at the same time. :) – Guilherme Sehn Oct 27 '13 at 22:36
  • For a more complete description of how `this` works see: http://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628 – slebetman Oct 27 '13 at 23:43
3

I think this example may make the difference clear. arguments is an array like object that contains each of the arguments passed to a function.

Try running each of these lines on your favorite browser console.

var somefunction = function() { console.log(arguments); };

Your first example demonstrates defining a named function a that closes around the named function somefunction.

var a = function() { somefunction(); };

Your second example makes a reference, b, directly to somefunction. This makes invoking b the same as invoking somefunction.

var b = somefunction;

Now if you call each of these a and b with some arguments you will see the difference.

=> a('a', 1);
[]

=> b('a', 1);
['a', 1]

In the first case the arguments object is empty. That's because the arguments that were passed to a were not forwarded onto somefunction.

In the second case the arguments are available to somefunction, because some function is being called directly.

Here is how you could redefine a so that it were functionally equivalent using apply

var a = function() { somefunction.apply(this, arguments); }

Running this at your console prints the argument array.

=> a('a', 1);
['a', 1]
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
2
var a = function(){
   somefunction();
}

Is an Anonymous Function attributed to a variable.

somefunction :function() {
  alert("hello");
}

Is an declaration of a function throungh the Object Literal notation.

The diference are shown when you are creating an object. The anonymous function are not acessible as a "public" method, instead in the Object Literal notation, that are acessible from outside.

As Douglas Crockford said, in JS the Good Parts, the first declaration are just a function and the second one could be a method.

Claudio Santos
  • 1,307
  • 13
  • 22
2

In the first case, you are creating a function which calls someFunction(), then you assign that function to a, so now calling a() calls an anonymous function which in turn calls someFunction().

In the second case, a and someFunction become the exact same thing, calling a() is the same as calling someFunction().

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
-1

The way you're setting var a by accessing the function is clearly out of scope. So I suspect you have a typo : instead of = :

var somefunction = function() {
  alert("hello");
};

somefunction(); // hello

...Now that your first and second makes sense with the code above:

Anonymous Function stored in variable:

var a = function(){
   alert('Hey');
   somefunction();
};
a(); // Hey // hello

Variable as Function Reference

var a = somefunction;
a(); // hello


In the other case than:
var objLiteral = {
  somefunction : function() {
      alert("hello");
  }
};

var a = objLiteral.somefunction;
a(); // hello
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313