Jason (jcreed) wrote,
Jason
jcreed

Javascript is funny.

An experiment was carried out in whichever version of firefox I happen to have. You may be able to achieve the same results with different values of the constants.

Experiment number 1:
x = 4; for (i = 0; i < 744; i++) { x = [x] }; try {x == ""} catch (e) {"exception raised"}
Result of evaluation: false.

Experiment number 2:
x = 4; for (i = 0; i < 745; i++) { x = [x] }; try {x == ""} catch (e) {"exception raised"}
Result of evaluation: "exception raised".

Can you guess why?

The exception raised is "too much recursion". Note well that there appear to be no function definitions or calls at all in the program as written, much less recursive ones.

Here is a (very, very strong) hint.

Experiment number 3:
x = {}; x.toString = function () { return "foo" }; x == "foo";
Result of evaluation: true.
Tags: javascript
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 11 comments