Newly revised Section 10 for ES3.1.

Richard Cornford Richard at litotes.demon.co.uk
Thu Jul 10 16:02:37 PDT 2008


> In the course of this, I noticed a number of conditions that plausibly
> might be restricted in the cautious subset, but currently aren't
> specified as such:
<snip>
> *        Illegal to assign to a top-level function name.
> Does anybody want to advocate for  including these restrictions
> in the cautious subset.

This last would not be a good idea in a language intended for browser 
scripting. There are circumstances were code needs to test its environment 
in order to determine how it should act. In many cases the conditions 
being tested for will not change while the script is being executed (the 
nature of a browser's object model will not change while it is executing a 
script) so repeating the test on each execution of a function is 
inefficient as the results of the test will always be the same as the 
first.

One strategy for dealing with that is to have the first call to a function 
perform the test and so decide which actions should be take in the given 
environment and, instead of taking those actions directly, assign a new 
function to replace itself where that new function only takes the chosen 
action and so avoids the overhead of testing on all subsequent calls to 
the function. Something like:-

fucntion getSomething(arg){
    if(caseOneTest){
        getSomething = function(x){
            //actions appropriate for case one
        };
    }else if(caseTwoTest){
        getSomething = function(x){
            //actions appropriate for case two
        };
    }else{
        getSomething = function(x){
            //actions appropriate for other cases
        };
    }
    return getSomething(arg);
}

It is the ability to do this sort of thing that helps make javascript so 
well suited to browser scripting. 




More information about the Es4-discuss mailing list