Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript detects and builds lost functions


May 09, 2021 CoffeeScript


Table of contents


Detect and build lost functions

Problem

You want to detect the existence of a function and build it if it does not exist. (for example, the ECMAScript 5 function of Internet Explorer 8).

Solution

Use the existing assignment operator (?) to assign a function to a prototype of the class library (using:: short case) and place it in an immediate execution function expression (do -gt;) to contain all the variables.

do -> Array::filter ?= (callback) ->
  element for element in this when callback element

array = [1..10]

array.filter (x) -> x > 5
# => [6,7,8,9,10]

Discuss

In JavaScript (similarly, in CoffeeScript), objects have a prototype member that defines what member functions can be applied to all objects based on that prototype.
In CoffeeScript, you can use :: shortcuts to access this prototype. S o if you want to add a filter function to an array class, execute Array::filter.... S tatement. It adds filter functions to all arrays.

However, don't overwrite a prototype that hasn't been constructed in the first place. F or example, if Array::filter is ... a lready exists in the browser in a fast local form, or the library manufacturer owns it for Array::filter . . The unique version, so that you either change to a slow JavaScript version or break this library that relies on its own Aray:: shuffle.
All you need to do is add a function when it doesn't exist. T his is the meaning of the existence of an assignment operator (?). I f we execute Array::filter . . s tatement, which first determines whether Array::filter already exists. I f it exists, it will use the current version. Otherwise, it will add your version.

Finally, since the assignment operators that exist create some variables at compile time, we simplify the code by encapsulating them in the Immediate Call Function Expression (IIFE). T his hides those internally specialized variables to prevent leakage. S o if the function we write already exists, then it will run, basically do nothing and exit, and it will never affect your code. B ut if the function we write doesn't exist, all we send out is a function that's closed. S o only the functions you write can have an impact on the code. Either way, the internal operation of ? . . is hidden.

Example

Next, we compiled CoffeeScript with the following method and attached the instructions:

// (function(){ ... })() 是一个 IIFE, 使用 `do ->` 来编译它。
(function() {

  // 它来自 `?=`  运算符,用来检查 Array.prototype.filter (`Array::filter`) 是否存在。
  // 如果确实存在,我们把它设置给其自身,并返回。如果不存在,则把它设置给函数,并返回函数。
  // The IIFE is only used to hide _base and _ref from the outside world.
  var _base, _ref;
  return (_ref = (_base = Array.prototype).filter) != null ? _ref : _base.filter = function(callback) {

    // `element for element in this when callback element`
    var element, _i, _len, _results;
    _results = [];
    for (_i = 0, _len = this.length; _i < _len; _i++) {
      element = this[_i];
      if (callback(element)) {
        _results.push(element);
      }
    }
    return _results;

  };
// The end of the IIFE from `do ->`
})();