Reputation: 11793
I saw below being used in the javascript.
var t = new Function;//what does it mean?
var t1 = new Function();
I also read Function in MDN. doesn't found any usage like this.
I knew Function is build-in object in the js. so I did some test for it . found the usage to object is not allowed. why?
var apple = {
type: "macintosh",
color: "red",
getInfo: function () {
return this.color + ' ' + this.type + ' apple';
}
}
var t3 = new apple;//Uncaught TypeError: object is not a function index.html:58
(anonymous function)
What does it mean for new Function
without brackets? thanks.
Upvotes: 1
Views: 133
Reputation: 1360
var a = new Function
this means you have created an empty anonymous function and both new Function and new Function() are the same if you do not provide the body of the function params
Also apple is an object so can't instantiate it.
Upvotes: 2
Reputation: 3362
The Function constructor(new Function/new Function())
creates a new Function object. In JavaScript every function is actually a Function object.
Both (new Function/new Function())
are same in java script
Please checkout Function
Upvotes: 1
Reputation: 484
Function is a constructor for creating an Object of type Function. u can create new object either one of these ways,
obj = new Function
or
obj = new Function()
The same way, If u have any constructor of ur own type u could do the same. Let's say u have some Constructor of Car. u can create new Car in either one of these ways,
myCar = new Car or myCar = new Car()
But, In ur case apple is constant object. u can not create new object using that. If u want u can have Apple Constructor as following,
function Apple(type, color) {
this.type = type;
this.color = color;
}
And then u could do what u actually wanted,
var t3 = new Apple("macintosh", "red");
Upvotes: 1
Reputation: 10849
new Function
is the same as new Function()
your problem here is independent.
apple
is a literal object, not a function, so you can't instanciate it, with or without brackets.
Upvotes: 2