Questions tagged [function-expression]

92 questions
96
votes
5 answers

Why use named function expressions?

We have two different way for doing function expression in JavaScript: Named function expression (NFE): var boo = function boo () { alert(1); }; Anonymous function expression: var boo = function () { alert(1); }; And both of them can be…
Afshin Mehrabani
  • 28,405
  • 26
  • 117
  • 186
92
votes
5 answers

Calling a javascript function recursively

I can create a recursive function in a variable like so: /* Count down to 0 recursively. */ var functionHolder = function (counter) { output(counter); if (counter > 0) { functionHolder(counter-1); } } With this,…
Samthere
  • 1,073
  • 1
  • 8
  • 12
11
votes
3 answers

Differences Between Named and Unnamed Anonymous Javascript Functions

Normally, in Javascript, when I want to pass an anonymous/inline function as an argument to another function, I do one of the following. someFunctionCall(function() { //... }); someFunctionCall( () => { //... }); However, I've recently…
Alan Storm
  • 157,413
  • 86
  • 367
  • 554
8
votes
1 answer

Why can’t I assign values to a variable inside a named function expression with the same name?

This is a named function expression with the name test. Inside, I assign 123 to a variable, also named test. Then test is logged. The function prints its body in the console, but not 123. What is the reason for such behavior? (function test() { …
7
votes
4 answers

JavaScript anonymous function expression vs IIFE

Encountered some code that's using IIFEs in an expression rather than just a normal function. var custom_type = (function() { return $('#myDiv').attr('custom_type'); })(); Normally I would write this as something like: var custom_type =…
diplosaurus
  • 2,334
  • 4
  • 19
  • 44
6
votes
1 answer

Function declaration or function expression

I just ran into a problem when defining a function in a block scope. Consider the following program: try { greet(); function greet() { alert("Merry Christmas!"); } } catch (error) { alert(error); } I expected this program…
Aadit M Shah
  • 67,342
  • 26
  • 146
  • 271
5
votes
1 answer

What's the difference between declaring functions using the fat arrow and non-fat arrow syntax in interfaces?

What's the difference between declaring functions using the fat arrow and non-fat arrow syntax in interfaces and types in TypeScript? For example: build(paramOne: string): string; Compared to: build: (paramOne: string) => string; At first, I…
Carven
  • 12,832
  • 24
  • 97
  • 139
5
votes
1 answer

Function Declaration vs Function Expression in the Module Pattern

I have just learned about the difference between Function Declarations and Function Expressions. This got me wondering about whether or not I'm doing things right in my AngularJS code. I'm following the pattern used by John Papa, but now it seems at…
4
votes
1 answer

Where is the immutable binding record of the identifier in a named function expression stored in JavaScript?

Recently I ran into some interesting facts about named function expressions (NFE). I understand that the function name of an NFE can be accessed within the function body, which makes recursion more convenient and saves us arguments.callee. And the…
4
votes
3 answers

In JavaScript, why can't I immediately invoke function declarations?

Only functions expressions can be immediately invoked: (function () { var x = "Hello!!"; // I will invoke myself })(); But not function declarations? Is this because function declarations are hoisted and already execute immediately? EDIT:…
4
votes
2 answers

What is the point of using a named function expression?

I'm going through this blog about the difference between function declarations and function expressions. It gives these two examples. They call the first an "anonymous function expression" and the second a "named function expression." // anonymous…
User314159
  • 6,447
  • 8
  • 31
  • 55
3
votes
2 answers

JavaScript: Is it okay to give a function the same name as the function expression?

In JavaScript, if I create a function expression like: var foo = function foo() { return 'Hello world!'; } Is this okay? Will there be any problems that occur? Or should I do: var foo = function baz() { return 'Hello world!'; }
Hadoren
  • 167
  • 2
  • 12
3
votes
1 answer

Why doesn't the location of a function expression matter in node js?

In JavaScript, when using a function expression (e.g. var myFunc = function() {...}), like any other variable declaration, you have to define the variable before using it. For example, the following example will not work (will result in Uncaught…
3
votes
2 answers

Hoisting function expression

As far as I know only the declaration part of a function expression gets hoisted not the initialization. E.g.: var myFunction = function myFunction() {console.log('Hello World');}; So "var myFunction;" gets hoisted, but "function myFunction()..."…
JShinigami
  • 4,503
  • 3
  • 9
  • 15
3
votes
1 answer

function expression on firefox - not expected result

if (true) { function foo(){ return 1; } } else { function foo(){ return 2; } } foo(); The above code is an example of function expression and returns 1 in Firefox 28 whereas 2 in Chrome ( expected result). Why is firefox giving wrong result ?
Vivek Chandra
  • 4,016
  • 8
  • 27
  • 37
1
2 3 4 5 6 7