SoFunction
Updated on 2025-03-01

Underscore function_Dynamic node Java Academy sorting

Because underscore is originally intended to give full play to the functional programming characteristics of JavaScript, it also provides a large number of higher-order functions that JavaScript itself does not have.

bind

What is the use of bind()? Let's first look at a common usage of errors:

'use strict';
('Hello, world!');
// Output 'Hello, world!'var log = ;
log('Hello, world!');
// Uncaught TypeError: Illegal invocation

If you want to use log() instead of (), it is not possible to follow the above method, because the pointer passed in by calling log() directly is undefined, and you must use it like this:

'use strict';
var log = ;
// Call the call and pass the console object as this:(console, 'Hello, world!')
// OutputHello, world!

How troublesome this is! It's better to use (). However, bind() can help us bind the console object directly to this pointer of log(). In the future, log() can be called directly and normally:

'use strict';
var log = _.bind(, console);
log('Hello, world!');
// OutputHello, world!

partial

partial() is to create partial functions for a function. What is a partial function? See example:

Suppose we want to calculate xy, we only need to call (x, y) at this time.

Suppose we often calculate 2y, and it will be more troublesome to write (2, y) every time. If we create a new function, we can write pow2N(y) directly like this. This new function pow2N(y) is a partial function created based on (x, y). It fixes the first parameter of the original function (always 2):

'use strict';
var pow2N = _.partial(, 2);
pow2N(3); // 8
pow2N(5); // 32
pow2N(10); // 1024

What if we don’t want to fix the first parameter and what should we do if we want to fix the second parameter? For example, if you want to create a partial function cube(x) and calculate x3, you can use _ as a placeholder to fix the second parameter:

'use strict';
var cube = _.partial(, _, 3);
cube(3); // 27
cube(5); // 125
cube(10); // 1000

It can be seen that the purpose of creating partial functions is to fix some parameters of the original function, which can reduce the difficulty of calling new functions.

memoize

If a function call overhead is very expensive, we may hope to cache the result so that we can get the result directly in subsequent calls. For example, calculating factorials is time-consuming:

'use strict';
function factorial(n) {
  ('start calculate ' + n + '!...');
  var s = 1, i = n;
  while (i > 1) {
    s = s * i;
    i --;
  }
  (n + '! = ' + s);
  return s;
}
factorial(10); // 3628800
// Pay attention to console output:// start calculate 10!...
// 10! = 3628800
usememoize()You can automatically cache the results of the function calculation:
'use strict';
var factorial = _.memoize(function(n) {
  ('start calculate ' + n + '!...');
  var s = 1, i = n;
  while (i > 1) {
    s = s * i;
    i --;
  }
  (n + '! = ' + s);
  return s;
});
// The first call:factorial(10); // 3628800
// Pay attention to console output:// start calculate 10!...
// 10! = 3628800
// The second call:factorial(10); // 3628800
// There is no output on the console

For the same call, such as factorial(10) is called twice in a row, the second call does not calculate, but directly returns the cached result after the last calculation. However, when you calculate factorial(9), it will still be recalculated.

Factorial() can be improved to make it call recursively:

'use strict';
var factorial = _.memoize(function(n) {
  ('start calculate ' + n + '!...');
  if (n < 2) {
    return 1;
  }
  return n * factorial(n - 1);
});
factorial(10); // 3628800
// The output result indicates that factorial(1)~factorial(10) has been cached:// start calculate 10!...
// start calculate 9!...
// start calculate 8!...
// start calculate 7!...
// start calculate 6!...
// start calculate 5!...
// start calculate 4!...
// start calculate 3!...
// start calculate 2!...
// start calculate 1!...
factorial(9); // 362880
// consoleNo output

once

As the name suggests, once() guarantees that a certain function is executed only once. If you have a method called register(), and the user can execute any of the two buttons on the page, you can use once() to ensure that the function is called only once, no matter how many times the user clicks:

'use strict';
var register = _.once(function () {
  alert('Register ok!');
});
// Test effect:register();
register();
register();

 delay

delay() can delay execution of a function. The effect is the same as setTimeout(), but the code is obviously simple:

'use strict';
// Call alert() after 2 seconds:_.delay(alert, 2000);
If the function to be called delay has parameters,Pass the parameters in:
'use strict';
var log = _.bind(, console);
_.delay(log, 2000, 'Hello,', 'world!');
// 2Print in seconds'Hello, world!':