java - Will new return the named function constructor instance-? -
var foo = function () { return new moo(); } var moo = function () { return this; }
if execute statement
new foo()
will instance of moo? seems @ both same time obvious , non-intuitive. functionally, should happen, @ same time not-expected if didn't know internals.
edit: realized seems unintuitive b.c. in java constructors can not return anything.
this similar constructor pattern jquery uses.
yes instance of moo.
non-intuitiveness because of fact can return other object in javascvipt constructor.this possible because functions in fact objects in js. in languages java , c# that's not possible, constructor returns object constructor belongs to. can cannot call contructors without new keyword in such languages. not returning constructor same thins return this;
in js thing(assuming it's used constructor) adds little confusion.
Comments
Post a Comment