Royi Namir
Royi Namir

Reputation: 148524

Function constructor initialization in JS?

How should I initialize Function constructor function :

(both seems to work.)

like this :

var t= new Function ("a","alert(a)");
t(3)//3
alert(Object.prototype.toString.apply(t)); //[object Function]

or

var t= Function ("a","alert(a)"); //without new 
t(3) //3
alert(Object.prototype.toString.apply(t));//[object Function]

Is there any difference ?

jsbin

Upvotes: 2

Views: 66

Answers (2)

loxxy
loxxy

Reputation: 13151

Both are same.

But if you use new, every property inside the object will have a new instance.

Upvotes: 1

James Allardice
James Allardice

Reputation: 165941

They are identical. From the spec:

When Function is called as a function rather than as a constructor, it creates and initialises a new Function object. Thus the function call Function(…) is equivalent to the object creation expression new Function(…) with the same arguments.

Upvotes: 3

Related Questions