Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - assigning function to variable [duplicate]

Tags:

javascript

As we all know, this following will not run the a() function so the alert box will not appear

// 1st
function a() {
  alert('A!');
  return function() {
    alert('B!');
  };
};

and we know that the following code will run the a() function and alert box 'A!' will appear

// 2nd
function a() {
  alert('A!');
  return function() {
    alert('B!');
  };
};
a(); // calling function

However, if we run following code, the a() function will be called and alert box 'A!' will also appear, just like the second code above

// 3rd
function a() {
  alert('A!');
  return function() {
    alert('B!');
  };
};
var x = a(); // assigning function to new variable

QUESTION: why does this happen (on 3rd snippet)? we didn't call the a() function yet (my current understanding). Didn't we just assigning x to a() function?.

like image 626
xcode Avatar asked Apr 16 '16 12:04

xcode


People also ask

Can you assign a function to a variable in JavaScript?

You can work with functions as if they were objects. For example, you can assign functions to variables, to array elements, and to other objects. They can also be passed around as arguments to other functions or be returned from those functions. The only difference with objects is that functions can be called.

How do you assign a function to a variable?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.

How do you make a copy of a function?

clone = function() { var newfun = new Function('return ' + this. toString())(); for (var key in this) newfun[key] = this[key]; return newfun; };

How to assign function to variable in JavaScript?

Assigning function to variable in JavaScript? Use return statement to assign function to variables. Following is the code − To run the above program, save the file name “anyName.html (index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.

How to assign an anonymous function to a variable in JavaScript?

The second method to assign the function to the variable is the arrow function. It is similar to the above approach, but the difference is that we will create an anonymous function without using the ‘ function ’ keyword and use an arrow instead.

How do you assign a function to another function in Python?

You can assign the reference to a function held by one variable to another by omitting the parentheses. This can result in an easy-to-make mistake: attempting to assign the return value of a function to another variable, but accidentally assigning the reference to the function.

Why do we need multiple variable assignments in JavaScript?

This tutorial teaches multiple variable assignments in JavaScript because variables are the most important part of our coding. Sometimes, we have to do many variable declarations and assignments as they have the same value. How? Let’s understand.


2 Answers

Didn't we just assigning x to a() function?.

No, you assigned the returned value from a() to x.

If you don't want to call a, then do

var x = a;

and later do

x();
like image 99
gurvinder372 Avatar answered Sep 30 '22 16:09

gurvinder372


You're wrong. You are invoking the function:

var x = a(); // assigning function to new variable

This line of code says invoke a when you write a(). The parentheses indicate an invocation.

To assign a function to a variable you have to use just the name, such as:

var x = a;

or pass the name to a function f:

f(a)

As a counter-example you invoke it in this next line of code and pass to g not the function be the result of its execution:

g(a())
like image 29
pid Avatar answered Sep 30 '22 17:09

pid