obsoleting the "new" keyword

Garrett Smith dhtmlkitchen at gmail.com
Fri Jan 30 15:08:46 PST 2009


On Fri, Jan 30, 2009 at 8:12 AM, Mark S. Miller <erights at google.com> wrote:
> On Fri, Jan 30, 2009 at 12:19 AM, Garrett Smith <dhtmlkitchen at gmail.com>
> wrote:
>>
>> No offense, but that is some ugly code.
>
> Indeed. We wouldn't have written it if the language didn't force us to.
>
>>
>> [...]
>> To supply varargs to that constructor, use a newApply function.
>>
>> Function newApply, for a constructor C:
>> 1) create a dummy function F
>> 2) assign the C's prototype to F
>> 3) create a new F, as i
>> 4) call C.apply(i, arguments)
>
> Your step #3 still fails for the reason I stated earlier:
>
>> Won't work on Array, Date, RegExp, and Function, since o is created with
>> [[Class]] "Object". But should work otherwise.
>

It would not work for Date, even if F were changed to Date. Example:

var d = new Date;
Date.apply(d, [1999, 11]);

Result is current date:
"Fri Jan 30 2009 10:42:08 GMT-0800 (PST)"

The applied object's [[Class]] is "Date" here. The problem is not
because the applied object's [[Class]] is "object". The reason it
doesn't work is that Date(), when called as a function, ignores the
arguments it is given and returns the current Date.

For creating Function, it would not work for another reason.

function f(){}
Function.apply(f, "a", "alert(a)");

That would not result in changing f. Instead, it would return a new
Function object.

It doesn't work for built-ins or Host object.

Garrett


More information about the Es-discuss mailing list