/    Sign up×
Community /Pin to ProfileBookmark

Functions as values

This is a fragment from the book “Beginning Javacript 5th edition”.
Chapter X :


————-

JavaScript is a powerful language, and a lot of that power comes from functions. Unlike many other languages, functions are rst‐class citizens in JavaScript; in other words, we can treat functions
just like any other type of value:

[CODE]function convertToCentigrade(degFahren) { var degCent = 5/9 * (degFahren – 32);
return degCent; }
var myFunction = convertToCentigrade;[/CODE]

This code assigns the

[CODE]convertToCentigrade() [/CODE]

function to the

[CODE]myFunction[/CODE]

variable, but look closely at the right‐hand side of the assignment.This means that we now have two ways of executing the same function. We can call it normally by executing convertToCentigrade(), or we can execute myFunction(), like this:

[CODE]
var degCent = myFunction(75); // 23.88888889
var degCent2 = convertToCentigrade(75); // 23.88888889[/CODE]


————-

What is the purpose of this handling?
What would be ideal/convenient situations for using the functions like that?

Thanks in advance!
Bruno.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 23.2017 — This is not a Q & A site for this purpose, it is for coders who have problems with their scripts and need help with a problem or pointing in the right direction.

If you need help with understanding JavaScript then I suggest that you start here : http://www.w3schools.com/
Copy linkTweet thisAlerts:
@krishna_agolaJan 23.2017 — Hi,

There is no meaning to assign same function to another function if you want reuse code then it just simply call because both are return same answer.

Thanks

krishna Agola
×

Success!

Help @brsastre spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...