Seb
Seb

Reputation: 47

How can you assign a var to a function and duplicate it?

Since the assignment operator can essentially assign variables to values/objects why cant you assign a variable to a function.

For example: When I assign var b = 5; b turns into 5, same for Objects when I create an instance. var b = c;

However when I assign var b = func(); it gives me the result or the return. Why does it do this and how do you make a duplicate function without the use of a closure or factory?

Upvotes: 1

Views: 31

Answers (1)

Quentin
Quentin

Reputation: 943936

The () characters are the syntax that means "call the function".

Remove them and you'll get the reference to the function object instead of calling it and getting the return value.

function func () {
    alert(1);
}

var b = func;
b();
setTimeout(func, 2000);

Upvotes: 5

Related Questions