Thursday, 15 July 2010

javascript - Cannot create Object of function defined in closure? -



javascript - Cannot create Object of function defined in closure? -

why can't create object function defined in closure?

var outer = function() { var constructor = function() { this.foo = 1; }; homecoming constructor; }; // should be: { foo: 1 }, is: undefined var constructorobject = new outer()(); // works var c = outer(); var constructorobject = new c();

you need wrap outer function phone call in parenthesis so:

var constructorobject = new (outer())(); // ^ ^ parenthesis here console.log(constructorobject); // constructor {foo: 1} console.log(constructorobject.foo); // 1

javascript

No comments:

Post a Comment