Question

I was experimenting on alert but alert dosen't worked as i expected have look below lines:

var tobealerted = function(){return 'worked!'};

now when i alert this:

alert(tobealerted());

this will work fine! now

alert(tobealerted);

in this i removed parentheses. now it alert function(){return "worked"} not worked! now i thought it may be a feature of javascript that it would alert any text without being in quotes then i wrote:

alert(worked!)

but the google console throw exception worked! is undefined , so my thinking was wrong. then again i wrote this

alert(function(){})

and you know what it alerted function(){}! and then i thought that alert would only allow such statement to be alerted without quotes. then again wrote:

alert(if(){}) //if is not defined
alert(for(){}) //for is not defined
alert(while(){}) //while is not define

but these did not worked for this i searched and found something, from my search i found this

functions are first class object

and according to wikipedia:

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures

so my question is

why did alert function only allowed function (){} to be alerted but not others

and if you think it is because that function is a first-class object then i don't think so because i did not assign this function(last one) to any variable neither it returns anything in alert(function(){})

i am so much curious and also confused! thanks!

Was it helpful?

Solution 3

Function is just another type of object in Javascript, so:

alert(tobealerted());

alerts the function result but:

alert(tobealerted);

alerts the function object itself using .toString() method which returns function's body for objects of type function.

Statements like if and so on are not objects but full definition of function declares an object. So you can alert function and not if statement for example.

OTHER TIPS

You gave the answer yourself. Functions are objects and as such have toString() functions. The other examples you gave are statements, not objects.

As noted by others, window.alert() only displays String objects and triggers the toString() function of any other type of object. It expects anything you feed it to be an object or a reference to an object.
This is what happened when you were trying to alert those statements; it assumed if etc. were variable names, but it couldn't find any reference by that name.

alert converts its argument to a string. That's all.

> x = function() { }
function () { }
> x.toString()
"function () { }"

You can't invoke toString() on an if/for/while as those expressions don't resolve to values.

If you want the alert to return a value it must be in quotes like this:

alert('worked!');

If you want it to display a value returned from a function, it must be called as a function:

alert(tobealerted());

If you call a function without the parenthesis, you are basically asking for the definition of the function and because functions are first-class, it will allow you to assign it to a variable.

The if/for/while are conditional operators so they don't work the same as a user-defined function. They have a very specific use and aren't meant to be overrode.

When you tried alert(function(){}); it was no different from you trying to assign the function to a variable. The function definition was stored, but you didn't actually invoke the function. Now if you did alert(function(){}());you would have actually invoked the function and it would have alerted undefined. It is all a matter of defining vs invoking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top