javascript - IIFE creation pattern - but how to support constructor parameters? -
javascript - IIFE creation pattern - but how to support constructor parameters? -
being longtime classical inheritance oo programmer i'm comfortable utilize of constructors , creating objects take required parameters constructor arguments. example, object sends alerts related order might like:
var ordernotifier = function(orderid, notifier, recipients) { this.notifyapproved = function() { // utilize notifier object passed ctor param send notifications // related orderid recipients } this.notifysomeotherevent = function() { // utilize ctor params 1 time again } } // utilize var on = new ordernotifier(12345, new basicnotifier(), somearrayofemails); on.notifyapproved(); so contrived illustration exemplifies (imho) value of parameterized constructor. specifically:
passing state object in single statement simplified method signatures internal state hiddenappreciating constructor creation pattern in javascript doesn't back upwards info hiding, attracted immediately invoked function expressions (iife) pattern closures , stronger access control. i've run against fact can't pass object construction parameters using iife pattern, or @ to the lowest degree don't understand how can.
i know can pass parameters anonymous function so:
(function(param){})(somevar); but isn't same explicitly creating new object , passing parameters constructor. nature of iife pattern don't have info pass object yet.
my iife version of above like:
var ordernotifier = (function() { var privatehelpermethod = function() { homecoming 'blahblahblah'; }; homecoming { notifyapproved: function(orderid, notifier, recipient) { // utilize notifier object passed ctor param send notifications // related orderid recipients var msg = privatehelpermethod() }, notifysomeotherevent: function(orderid, notifier, recipient) { // utilize ctor params 1 time again var msg = privatehelpermethod(); } }; }}(); so must inquire experienced, progressive javascript pros: using standard ecmascript 5 language features, best practice (or maybe mutual practice) of creating object iife pattern , providing object state in single operation? (other exposing setstate() or similar method.) in other words: how can have cake , eat too?
your iife constructor pattern creates singleton (only 1 object, ever). doesn't expose constructor can't used create more objects. such, can pass info want singleton via arguments iife or code arguments right implementation.
this why wanted show design pattern talking because particular pattern isn't general purpose constructor , isn't designed create multiple objects. creates singleton object. that's best for. if returned object exposed functions used constructors, take arguments other constructors.
i don't see problem here. don't see there issue solved.
per comments, if want pass non-static info iife, either have locate iife after creation of non-static info (so info available before iife runs), select different design pattern (e.g. utilize traditional constructor) or utilize iife creates constructor (which can phone call later), rather object.
for example, here's iife creates constructor:
var ordernotifier = (function() { // private stuff go here // shared instances homecoming function(/* constructor args go here */) { // per-instance private vars here homecoming { notifyapproved: function(orderid, notifier, recipient) { // utilize notifier object passed ctor param send notifications // related orderid recipients }, notifysomeotherevent: function(orderid, notifier, recipient) { // utilize ctor params 1 time again } }; } }}(); // sometime later in code var notifier = new ordernotifier(/* args here */); javascript
Comments
Post a Comment