obsoleting the "new" keyword

Peter Michaux petermichaux at gmail.com
Sat Jan 24 21:17:57 PST 2009


On Wed, Jan 14, 2009 at 12:00 PM, Mark S. Miller <erights at google.com> wrote:
> On Wed, Jan 14, 2009 at 11:58 AM, Juriy Zaytsev <kangax at gmail.com> wrote:
>>
>> Are there any plans to include such facility in ES3.1 (or any objections
>> against it?)
>
> Definitely not in ES3.1 -- it's too late.
>
> Possibly for ES-Harmony, if someone posts a fleshed out proposal (hint hint
> ;)).

As an idea, I just ran the following code in Firefox and it did do
what I expected. It makes it possible to pass around a plain function
object that will construct.

Function.prototype.__defineGetter__("make", function() {
  var thisC = this;
  var F = function() {};
  return function() {
    F.prototype = thisC.prototype;
    var o = new F();
    thisC.apply(o, arguments);
    return o;
  };
});

function Foo(a) {
  this.a = a;
}
Foo.prototype.greet = function() {
  alert('a foo says hello ' + this.a);
};

var f = Foo.make('a');
f.greet();

(function(con) {
  var ff = con('asdf');
  ff.greet();
})(Foo.make);


------

The above is not compatible with code written like the following

function Foo() {
  if (this instanceof Foo) {
    //...
  }
  else {
    //...
  }
}

To be compatible with this type of constructor, the following works
when there are no arguments to the constructor

Function.prototype.__defineGetter__("make", function() {
  var thisC = this;
  return function() {
    return new thisC();
  };
});

Is there a way to write "make" without using eval so that the
constructor is called with an arbitrary number of arguments?

Peter


More information about the Es-discuss mailing list