No. There is indeed no harm of calling Function
without the new
keyword. In fact, I would advise against using new
in your programs as much as possible: stop using the new
keyword.
It would be much better to create a new
function instead:
Function.prototype.new = function () {
Factory.prototype = this.prototype;
return new Factory(this, arguments);
};
function Factory(constructor, args) {
return constructor.apply(this, args);
}
The advantage of doing so it that now you can create instances as follows:
function Foo(a, b) {
this.a = a;
this.b = b;
}
var foo = Foo.new(1, 2);
// instead of
var foo = new Foo(1, 2);
What's the point? The point is that you can now do things like:
var foo = Foo.new.apply(Foo, [1, 2]);
Because new
is a function you can do things to it like apply it to an array of arguments, etc. You can't do the same thing with the new
keyword.
Anyway, getting back to the topic at hand the Function
constructor can be called with or without the new
keyword. It doesn't make any difference. Hence I would advise you not to use the new
keyword at all. Not only does it save you a few keystrokes but also it can be used for things like:
Function.apply(null, ["a", "b", "return a + b"]);
This is especially useful if you don't know how many arguments you want the function to have beforehand.
Hope that helps.