ARGUMENTS.SHOULD.BE.ARRAY bug-fix

Vassily Gavrilyak gavrilyak at gmail.com
Mon Mar 19 03:20:58 PDT 2007


On 3/19/07, Brendan Eich <brendan at mozilla.org> wrote:
> On Mar 18, 2007, at 4:20 PM, Vassily Gavrilyak wrote:
>
> > Well, I sometimes get confused. It's very rare cases, but still
> > happens to me. Don't remember any C-like language, but our cousin
> > VBScript
> > allows this. And those guys are sometimes confused too.
> > Like here. http://www.thescripts.com/forum/thread91618.html
>
> Thanks, good information.
>
> > That's 'very corner' case, but why not just fix it, if the fix will
> > not break anything?
>
> You're certainly right that this is a syntactic extension that won't
> break any existing code. Jeff Dyer should comment, since he's the
> grammar owner and has good taste ;-).
>
> /be
>
>
Thank you for considering this. While I can imagine why this
functionality was disabled in ES1-3 (to protect programmer from
misprints), in typed language typer could be responsible for handling
this, not grammar.
Well, let's wait for Jeff to comment.

>Note also how arguments' type may not be Array, but something
>delegating to Array.prototype. We need to finalize the spec, but
>already ES1-3 makes arguments a magic type that aliases >arguments[0]
>to a in function f(a){...}.
So we have a magic that makes position->name transition for arguments.
We also have destructuring magic. Shouldn't we go
further and add desctructuring magic to function call too? This way we
will get named arguments functionality, and that's something that
adds readability to code in some cases.
We can do emulate it right now (and I already do it) with hashes.
But having this magic in language will help to optimize such calls and
add readability.

Example

function Person(name, age){
    this.name = name;
    this.age     = age;
}

Now I can use it like
person = new Person("Joe", 20)

But much readable that I prefer to use is
person = new Person(name:"Joe", age:20);

I can simulate this with
person = new Person({name:"Joe", age:20})
and pay performance penalty for readability,
so my constructor will become something like this.

function Person(name, age){
    if(typeof(name) == "object"){
       var obj = name;
        this.name = obj.name;
        this.age = obj.age;
    }else{
       this.name = name;
       this.age     = age;
    }
}

Again, not a big issue, but a little speed-up for correct and readable
code will help.
Probably named parameters were already considered, in this case I'm
sorry for disturbing, just failed to find it in spec.


Regards,
Vassily Gavrilyak



More information about the Es4-discuss mailing list