Fredo Corleone
1 min readMay 18, 2018

Hi good people!

Why not using factory functions to achieve the same thing that decorators do?

Look at my code to achieve your same Batman example:

const comicBookCharacter = ( first, last ) => {
const firstName = first
const lastName = last
return {
realName: `${firstName} ${lastName}`
}
}
const superPower = ( ref ) => {
let powers = []
return {
addPower: ( p ) => {
powers.push( p )
return ref
},
powers
}
}
const utilityBelt = ( ref ) => {
let utilities = []

return {
addToBelt: ( p ) => {
utilities.push( p )
return ref
},
utilities
}
}
const decorate = ( obj, factoryArray ) => {
const instances = factoryArray.map( f => f( obj ) )
return Object.assign( obj, …instances )
}
let batman = comicBookCharacter( ‘Bruce’, ‘Wayne’ )console.log( batman.realName )decorate( batman, [ superPower, utilityBelt ] )batman
.addPower( ‘money’ )
.addToBelt( ‘something’ )
.addPower( ‘strength’)
.addToBelt( ‘batarang’ )
console.log( batman.powers, batman.utilities )

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Fredo Corleone
Fredo Corleone

Written by Fredo Corleone

Ex web-app developer. Now just a free man!

No responses yet

Write a response