Questions tagged [function-expression]

100 questions
1
vote
1 answer

Why does the "typeof" of a named function expression return undefined?

I'm new to JS, so please forgive me if this sounds stupid. I was playing with the concepts of function declaration and function expression. I have the following code: var printSomething = function printSomeString(string) { …
Auro
  • 1,578
  • 3
  • 31
  • 62
1
vote
1 answer

Runnable class is not exectued

I implemented a virtual counter, just counting up and down between 0-100. It's very Simple, the factory supplies a VirtualCounter which implements Runnable. @Getter @Setter public class VirtualTimer implements Runnable { private int…
Anna Klein
  • 1,906
  • 4
  • 27
  • 56
1
vote
0 answers

Why can i invoke a variable in JavaScript as a function due to the Function Expression variety?

const result = (numberOne, numberTwo) => { if(numberOne > numberTwo){ return true; } else { return false; } }; console.log(result(1, 2)); // here i use the variable as a function?! So why can i use a variable as a…
1
vote
0 answers

What exactly is a function expression in Javascript?

So I found this terminology a little bit confusing. I normally see tutorials talking about function expression with an variable assigned to a function, but also, from Mozilla's official website function expression, it says: A function expression is…
Hang Chen
  • 549
  • 1
  • 7
  • 18
1
vote
1 answer

Difficulties with using function expression

Hopefully you will answer it ! var myform = document.getElementById("myform"), saveBtn = document.getElementById("submit"); saveBtn.addEventListener("click", saveInfo); var saveInfo = function (e){ e.preventDefault(); var dateValue =…
1
vote
3 answers

Struggling to Understand the Parameter Content when using .map on Function Expressions

So I'm looking at function expressions. This is the code I'm playing with: var modifiedNames = [ "Thomas Meeks", "Gregg Pollack", "Christine Wong", "Dan McGaw"…
phurst-so
  • 386
  • 5
  • 15
1
vote
2 answers

Is it possible to have parameters for anonymous function?

Given var stuffs = [ { id : 1, name : "orange"}, { id : 2, name : "apple"}, { id : 0, name:"grapes"} ]; var filterMethod1 = new function(o){return (o.id>=1);}; // this gives undefined error for o function filterMethod2(o) {return…
1
vote
2 answers

function declaration and function expression performance difference

I have used JSperf to test a small sample of code. According to a few articles I came across, both should have similar performance with test2 having a little edge. But here it's the complete opposite. Can someone explain why the huge…
Pardha.Saradhi
  • 468
  • 1
  • 10
  • 27
1
vote
1 answer

Can isNaN be used as identifier in a named function expression?

The context: I am reading the book You Don't Know JS: Up & Going, Chapter 2: Into JavaScript. In the Polyfilling section, the author gives the following example: if (!Number.isNaN) { Number.isNaN = function isNaN(x) { return x !== x; …
1
vote
2 answers

for loop assigns same functon everytime in Livescript

I would expect "x's" results, "y's" results and "z's" results be the same: In Livescript: x = a: -> 3 b: -> 4 y = {} for k, v of x console.log "key: ", k, "val: ", v y[k] = -> v.call this console.log "y is: ", y console.log "x's:…
ceremcem
  • 3,900
  • 4
  • 28
  • 66
1
vote
1 answer

Anonymous function expression in Google Chrome

I am using Google Chrome version 52 64-bit. I found out that if I use anonymous function expression ex. // Anonymous function expression var expressionFunc = function(){ return true; }; The variable expressionFunc will hold the assigned…
1
vote
2 answers

Function expression in JavaScript confusion

I'm trying to make sense of this code from another developer and my JavaScript knowledge is lacking. This function is supposed to take the header menu of a site and convert it into a mobile style menu. I understand why jQuery is being passed in as…
Anne Lee
  • 107
  • 1
  • 8
1
vote
0 answers

In JavaScript, Why function expression needs to be in bracket for calling the function

I tried calling function(v){alert(""+v);}(4); But warning came out function statement requires a name When I called using name function fname(v){alert(""+v);}(4); Nothing happenedBut when I called using…
1
vote
1 answer

What are good situations to use function expression instead of function declaration?

I will prefer to use function declaration all the time because I can place the function anywhere on the source file. If I use function expression, the function has to be placed at the top of the source file. Are there good situations to use function…
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
1
vote
3 answers

Javascript private methods: function expression vs function declaration

A common method of creating private methods (of sorts) in javascript is this: Class = function (arg0, arg1) { var private_member = 0; var privateMethod = function () { return private_member; }; } The above example could also…