obsoleting the "new" keyword

Garrett Smith dhtmlkitchen at gmail.com
Fri Jan 30 00:19:54 PST 2009


2009/1/24 Mark S. Miller <erights at google.com>:
> On Sat, Jan 24, 2009 at 9:17 PM, Peter Michaux <petermichaux at gmail.com>
> wrote:



>> Is there a way to write "make" without using eval so that the
>> constructor is called with an arbitrary number of arguments?
>
> In Harmony you'll be able to use the "..." operator.
>
> In ES3 and ES3.1, in order to handle Date, RegExp, and Array, the Cajita
> runtime includes Mike Samuel's "triangle of hackery":
>

No offense, but that is some ugly code.

For user defined function, you can "hide" the constructor function in
a function then export a "create" method that calls "new".

var pkg = {};
(function(){
 pkg.createC = createC;
 function createC(s) {
   return new C(s);
 }
 function C(s){
   this.s = s;
 }
})();

pkg.createC('s');

The constructor can still be gotten by the constructor property of a
created instance, so it can be broken, but only if the user tries.
Lots of plumbing there, too.

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)

Garrett


More information about the Es-discuss mailing list