A convention has sprung up recently to make pure functions in Javascript. To get purity when dealing with arrays or objects cloning has been encouraged. But the nuance of Javascript and references is tricky. To get it right we need to understand references, mutations, shallow clones and deep clones. Which is quite a mouthful and is easy to trip over when coding.
TLDR
Know when we’re mutating to use shallow clones and avoid deep clones for internal functions.
Having been in the Javascript community for a while I’ve witnessed the building momentum towards a Functional Programming (FP) style. I think Javascript and FP are great but there is perhaps a miss-guided favor or emphasis put on FP as something superior. The React community help perpetuate this with the introduction of hooks, moving away from class based components (favoring functions) and libraries like Redux all employing bits of FP.
I’ve been building a component for Joomla 1.5 and I really wanted to use Jquery in the admin over Mootools. Unfortunately Googling for this answer is tricky as so many people are posting examples how to remove Joomla js from their frontend template. The following is very ‘hacky’ but if you’re feed up with the Joomla API and want to throw together a component, without mootools, it’s useful. It stops the need to export jQuery into a different name space, therefore breaking 3rd party jQuery plugins.
A quick note on converting javascript time stamps to human-readable format. I had been working on a travel calculator with new Google Maps v3 which required this for UI.
The following function takes a javascript Date object as a parameter and converts it to a human-readable string.
The functions themselves are simplistic. But can be tedious to work out in a pinch. Also, the pluralise function is a good starting point for converting other values to human-friendly names. While it won’t cover all edge cases it services the majority.