A function that tells itself

S

Stone Zhong

Tired of reading, and make some fun :)

var a = function Foo() { return Foo; }();

alert(a);
alert(a());
alert(a()());
alert(a()()());

all outputs are "function Foo() { return Foo; }"
 
D

Dmitry A. Soshnikov

Tired of reading, and make some fun :)

var a = function Foo() { return Foo; }();

alert(a);
alert(a());
alert(a()());
alert(a()()());

all outputs are "function Foo() { return Foo; }"

Ah, so you've "invented" "self-reproducing" (or "self-replicative")
functions -- http://bit.ly/dacAAg ;)

Dmitry.
 
T

Thomas 'PointedEars' Lahn

Stone said:
Tired of reading, and make some fun :)

var a = function Foo() { return Foo; }();

You want to avoid named function expressions.
alert(a);
alert(a());
alert(a()());
alert(a()()());

all outputs are "function Foo() { return Foo; }"

The explanation why that is so, i.e. why passing a reference to a Function
instance to the `alert' method of Window instances may result in displaying
the code of the referred function is left as an exercise to the reader.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top