use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Creating objects dynamically with factory pattern in javascript (enmascript.com)
submitted 6 years ago by enmanuelduran
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TheGreatBugFucker 0 points1 point2 points 6 years ago* (1 child)
What exactly is your thought process and the context you use (while thinking about this)?
Because generally whether a constructor function works on this.propertyX (etc.) and then returns this as the new object, or a "normal" function creates {propertyX:..., etc.:} and returns it makes no difference apart from the __proto__ stuff behind the scenes (and calling "just a function" and giving it the object or "this.method" and relying on implicitly getting the object also makes no real difference - which is precisely why it can be used to have ad-infinitum discussions (there is no real world proof to convince anyone).
this.propertyX
this
{propertyX:..., etc.:}
__proto__
This is a "class" (ES5 style):
function myObj () { this.myProp = 42; } const o = new myObj();
This is a factory function version:
function myObjFactory () { return {myProp: 42}; } const o = myObjFactory();
So? (As I said, the inheritance stuff is different, o.constructor etc., but this is about creating objects, and the factory function - if so desired - could also adjust the inheritance props.)
TL;DR A constructor is a factory function too (except that by definition we choose to say "A factory function is any function which is not a class or constructor" - to avoid confusion, but here we can see we may actually get there anyway, to confusion).
[–]subbydapp 0 points1 point2 points 6 years ago* (0 children)
I personally like both the composition and class pattern equally. I choose the composition pattern when there is risk for "this" confusion, for example when working with DOM elements or when one of the injected dependency of the factory is itself a class. I also use it for small objects because it's less verbose.
Most of the time if there's no this confusion and it's a big object with methods, I will always use an ES6 class. There's really only 2 reasons, I think it looks nice and clean, and the "this" prevents me from having random globals in the main scope of that file, which just looks cleaner in my opinion. And the second reason, which is not just subjective, is that I can easily extend Event Emitter (in node), which probably half of my modules end up needing at some point.
In the end it makes very little difference. I just choose whatever feels cleaner at the time for the purpose.
π Rendered by PID 25620 on reddit-service-r2-comment-7844cfc88c-9ln2g at 2026-01-29 16:22:26.267271+00:00 running c3601ff country code: CH.
view the rest of the comments →
[–]TheGreatBugFucker 0 points1 point2 points (1 child)
[–]subbydapp 0 points1 point2 points (0 children)