javascript - "new" Before Anonymous Function Invocation Returning Object -


this question has answer here:

i'm reading knockoutjs source code.

i ran following line i'm not sure understand...

ko.utils = new (function () { 

generally, structure seems along lines of:

ko.utils = new (function () {     // variables declared var     return {         export:value,         export:value    };  })(); 

i don't understand construct, why new needed? do? useful for?

(i thought if function called new before name invoked constructor, , if returns object it's identical invokation without new.)

update: asked knockoutjs team on github , got back:

my guess steve didn't know wasn't needed. looking @ original commit, see lot of unnecessary news have since been removed.

it might pattern prevents this reach global context (not in case, since every variable declared var, author might wanted use general pattern create objects).

var x = new (function () {    this.foo = "bar";    return {         // whatever    }; })();  console.log(foo); // uncaught referenceerror: foo not defined   var x = (function () { // without new    this.foo = "bar";    return {         // whatever    }; })();  console.log(foo); // "bar" 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -