javascript - Using Object constructors that return objects. How it works? -



javascript - Using Object constructors that return objects. How it works? -

i've been creating js objects a = {} or a = new myconstructor() without thinking much it.

then came code looks this:

function constructor(){ var private = { a:1, b:2 }; homecoming private; } var = new constructor();

a naturaly contains new instance of private object. , realised new operator not required because private object gets created every time constructor function gets called.

so actual question is: happens when calling new constructor()?

why shouldn't a = constructor() instead?

what happens public properties of constructor object if any?

is returning objects constructor bad idea?

what happens when calling new constructor()?

have @ the docs new operator , what 'new' keyword in javascript? question.

an instance created (like object.create(constructor.prototype)), accessible via this within constructor function. however, result of phone call returned object (private), assigned a.

why shouldn't a = constructor() instead?

actually, should this. constructor not constructor, doesn't create instances - returns private object. therefore, don't utilize new. however, should rename function clear, e.g. makexyz().

what happens public properties of constructor object if any?

a , b "public" properties of returned objects. if mean "static" properties on constructor function itself, nil happens them. if mean "shared" properties on constructor.prototype - nil happens them, not inherited returned objects (which not intended).

is returning objects constructor bad idea?

from constructors, yes. functions in general, no. create normal factory function (i.e. function returns objects), fine pattern.

javascript oop

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -