I get the use of JavaScript's new
operator, anonymous functions, and use of functions as first class citizens (i.e. passing functions as parameters, using functions as return values, etc..) And I thought of doing a weird combination of these.
function foo(s) {
return function () {
alert(s);
}
}
var bar = foo('bar');
var baz = new foo('baz');
bar();
baz();
Both calls bar()
and baz()
behave similarly (i.e. alert's 'bar'
and 'baz'
respectively).
Is there really a difference between variable = func()
vs variable = new func()
when func()
returns an anonymous function?