Detecting and Creating Missing Functions
You want to detect if a function exists and create it if it does not (such as an ECMAScript 5 function in Internet Explorer 8).
Use the existential assignment operator (
?=) to assign a function to the classes’ prototype (using the
:: shorthand), and wrap it all in a IIFE (
do ->) to contain the variables.
In Coffeescript, you access this prototype using the
:: shortcut. So, if you want to add a filter function to the array class, you do
Array::filter = .... This will add the filter function to all arrays.
However, we don’t ever want to overwrite a prototype that we haven’t created in the first place. For example, if
Array::filter already exists in a fast native form in the browser, or a library maker has their own specific version of
What you need to do is only add the function if it doesn’t already exist. That’s where the existential assignment operator (
?=) comes in. If we do
Array::filter ?= ... instead, it will see if
Array::filter already exists. If it does, then it will use the current version. If it doesn’t, it will add yours.
Finally, because the existential assignment operator–when compiled–creates a few variables, we clean up the code by wrapping it in an Immediately-Invoked Function Expression (IIFE). This hides those internal-use-only variables from leaking outside. So, if the function we’re writing already exists, it runs, does basically nothing, and exits, affecting absolutely none of your code. But, if the function we’re writing doesn’t exist, we send out only the function we’re writing as a closure, so only the function you’ve made affects the code. The internal workings of
?= are hidden either way.
Below, we’ve compiled and annotated the coffeescript written in the solution above