3 ms·
IMHO for the most part, programming with classes in JS is just waiting for pain. If you use generic objects for state, then compose your applications by using
by tracker1 12y ago
IMHO for the most part, programming with classes in JS is just waiting for pain. If you use generic objects for state, then compose your applications by using generic functions passing in state, and any other arguments that function needs to operate, it goes much smoother than OOP.
You can create wrappers by binding independent methods against said state for composite objects with encapsulation... It tends to keep code cleaner and more modular. Sometimes just thinking about the problem differently will lead to a more elegant solution...
var Promise = require('i-promise')
,getDealerPendingStream = require('./process-get-dealer-pending-stream')
,transformDealerPending = require('./process-transform-dealer-pending-stream')
,calculatePayment = require('./process-payment-calculation')
,logTransaction = require('./process-log-transaction')
,updateSalesforce = require('./process-update-salesforce')
,runPayment = require('./process-payment')
,logItemOutput = require('./process-log-item-output')
;
module.exports = function(){
return new Promise(function(resolve, reject) {
getDealerPendingStream()
.pipe(transformDealerPending())
.pipe(calculatePayment())
.pipe(runPayment())
.pipe(logTransaction())
.pipe(updateSalesforce())
.pipe(logItemOutput()) //email notifications for processing errors (dataObject.error)
.on('error', reject) //should never happen
.on('end', resolve)
.resume() // needed so last stream can process data
;
});
}
- aikah 12y agoHow would you write a GUI framework with this method? because that's where OOP shines.Here,you're just doing sequential operations on a chunk of data.How would you write layouts and widgets? people often forget that OOP was kind of created because it solves the UI problem.
- masklinn 12y agoSame way Elm does?
- tracker1 12y agoYou can still uses events and state against UI constructs without going full-on OOP. An application can render in similar ways to how flux, mercury, elm and other patterns are handled. (not suggesting a virtual DOM... but there's more to an application than the UI alone, and there are more functional ways to apply state and eventing to a UI. uicontrol.on('eventName', handleSomeEvent.bind(null, state, uicontrol)); In this case, you still have a uicontrol, that raises events, and can use that to trigger a generic handler pre-bound against a given state. You can remove this farther by having the handleSomeEvent being a method that is already part of a state machine that is bound against an instance of said state. If you follow 1-way dataflows (like flux) you can break some of the other constructs down... just the same, the above example is not OOP, but can still use UI events, and be composable.