Questions tagged [function-expression]
100 questions
0
votes
0 answers
Variable declaration is a statement but why function expression is not?
I can not understand the difference between statement and expression. I think there is always something missing because I can't understand well the meaning of expression evaluates to a value.
For example, we say about variable declaration a…

Muhammad Hossam
- 55
- 9
0
votes
1 answer
toggling variable inside Module Pattern using function declaration vs function expression
I have a module pattern with a variable setting the currentPlayer to 1. I use a function expression to toggle that variable
const game = (() => {
let currentPlayer = 1;
const toggleCurrentPlayer = () => {
if (currentPlayer == 1){
…

spacing
- 730
- 2
- 10
- 41
0
votes
1 answer
when function expression to declare class how can I define type?
const Foo: new () => any = class {
constructor() {}
bar(): string {
return ‘Hello World!’;
}
};
const instance = new Foo();
below I want to replace any cause my config not allows any
new () => any
How can I define Foo()'s result ?

sunhee kyung
- 69
- 7
0
votes
3 answers
Why does my function call only work when the function is an expression and not when it's a function declaration?
Learning about closures, I wrote a function that returns a function which prints out a name to the console:
let myName = 'steven';
function printName() {
console.log(`OUTER: ${myName}`);
return function printAgain() {
…

Casey Cling
- 401
- 2
- 5
- 15
0
votes
1 answer
Is an anonymous function as a parameter a function declaration or a function expression?
Assuming a function declaration is a statement where the function keyword is the first word of the statement, e.g.:
function() { console.log("foo") };
Assuming that a function expression is e.g. following:
for a named function
var func = function…

quizmaster987
- 549
- 1
- 6
- 15
0
votes
2 answers
array.filter() removes values in a function expression but not in arrow function
I'm solving a problem where a function array_diff returns values in arraya that are also in array b.
Since I've been learning about named function expressions being better for console debugging than anonymous fat arrow functions, I'm trying to solve…

abecodearian
- 3
- 2
0
votes
0 answers
combining function declaration and expression doesn't work
I was reviewing function declaration and function expression, separately, no problem
function add(a,b){return a+b};
var add2 = function(a,b){return a+b};
add(1,2)// 3
add2(1,2) // 3
But if I try to combine them the function declaration gets…

Jason Rogers
- 19,194
- 27
- 79
- 112
0
votes
1 answer
Prevent Exit Pop Up Loading After Form Submit
My aim is to have a exit pop up which triggers the window.onbeforeunload if somebody tries to close the current tab or browser. But after they complete a sign up form to opt in to my e-mail list and redirect to my "Thank you page URL", I do not want…
0
votes
1 answer
Why do function declarations get hoisted and function expressions don't?
According to hoisting definition:
Hoisting is a JavaScript mechanism where variables and function
declarations are moved to the top of their scope before code execution
Why do function declarations get hoisted and function expressions don't?

Priyank
- 3,778
- 3
- 29
- 48
0
votes
1 answer
How can I set named function expressions on my Dropzone options?
I have multiple Dropzones in my project that are all very similar. On one of them, I had to create a minimum file width, but the video Dropzone doesn't work with the file width code. I ended up having to create a separate options call for each…

Rachel Martin
- 525
- 6
- 18
0
votes
0 answers
Why this function expression execute when first assigned?
I suddenly feel so confused about this example:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval
in the script,
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t =…

Ying
- 299
- 1
- 5
- 20
0
votes
0 answers
Is it correct to say that the function keyword is essentially obsolete in JavaScript?
I want to see if my understanding is correct: the functionality of the function keyword was divided up between classes and Arrow functions in es6.
First, in the case of anonymous functions, I see no advantage in using function() {...} over Arrow…

Samasambo
- 113
- 7
0
votes
0 answers
Does functions expressions assume the this of the lexically enclosing function context?
I confident about that function declarations does not has lexical context and arrow function it does have, but I am confused if function expression does ?
According to what I read it doesn't but in the following example it works fine both ways(…

CodeHip
- 367
- 5
- 18
0
votes
1 answer
AngularJS Provider doesn't accept ES6 function declaration
I've been trying to rewrite some code of ours to comply with ES6 and have encountered the following issue.
angular.js:63 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:pget] Provider 'books' must…

Jimenemex
- 3,104
- 3
- 24
- 56
0
votes
0 answers
What are the pros/cons and best practices for creating functions for a React Component class?
Both of these classes do the same thing, but ClassA seems redundant because of the binding you have to do every time you declare a function and ClassB doesn't seem as clean as ClassA because of the differences in types of function creations.
class…

Vongdarakia
- 379
- 1
- 12
- 25