2009/1/24 Mark S. Miller <erig...@google.com>:
> On Sat, Jan 24, 2009 at 9:17 PM, Peter Michaux <petermich...@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
_______________________________________________
Es-discuss mailing list
Es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to