Functional Sharing in JavaScript

kyle kelly
2 min readApr 29, 2019

--

Functional sharing instantiation in JavaScript is very similar to Functional instantiation. With Functional inheritance in JavaScript, you create an object literal inside of the function. Whenever we call that function, it creates a new object and returns it. However, if you were to create a new object using this method, you would duplicate all properties and methods in memory.

So if one of the downsides to to functional instantiation is duplicating properties and methods in memory, the Lo-dash library provides a nifty function called extend. Extend attempts to overcome duplicating memory, and making all the methods shared among all objects.

Just like Functional instantiation, we start with a function with an empty object and fine properties within that function. Methods are defined in another object and then we use extend on our object to extend to our methods. Every object created by functional shared will have a pointer to the same methods without duplication.

What if you didn’t have access to the extend method that Lo-dash library provides? You can still use the Functional shared instantiation by putting the properties and methods in two separate object. Instead of using extend, we could just take the methods and reference them where we defined our properties in our object.

Though, functional sharing instantiation takes care our problem of duplicating methods, it still leaves us with issues. Pointers to the shared methods are created when the object is instantiated. So if we were to modify the methods and then new objects, the original object and the new objects would be referring to the different methods.

--

--