58、JavaScript underscore Functions

因为underscore本来就是为了充分发挥JavaScript的函数式编程特性,所以也提供了大量JavaScript本身没有的高阶函数。

 

bind

bind()有什么用?我们先看一个常见的错误用法:

  1. 'use strict';
  2. console.log('Hello, world!');
  3. // 输出'Hello, world!'
  4. var log = console.log;
  5. log('Hello, world!');
  6. // Uncaught TypeError: Illegal invocation

 

如果你想用log()取代console.log(),按照上面的做法是不行的,因为直接调用log()传入的this指针是undefined,必须这么用:

  1. 'use strict';
  2. var log = console.log;
  3. // 调用call并传入console对象作为this:
  4. log.call(console, 'Hello, world!')
  5. // 输出Hello, world!

 

这样搞多麻烦!还不如直接用console.log()。但是,bind()可以帮我们把console对象直接绑定在log()的this指针上,以后调用log()就可以直接正常调用了:

  1. 'use strict';
  2. var log = _.bind(console.log, console);
  3. log('Hello, world!');
  4. // 输出Hello, world!

 

partial

partial()就是为一个函数创建偏函数。偏函数是什么东东?看例子:

 

假设我们要计算xy,这时只需要调用Math.pow(x, y)就可以了。

假设我们经常计算2y,每次都写Math.pow(2, y)就比较麻烦,如果创建一个新的函数能直接这样写pow2N(y)就好了,这个新函数pow2N(y)就是根据Math.pow(x, y)创建出来的偏函数,它固定住了原函数的第一个参数(始终为2):

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

 

如果我们不想固定第一个参数,想固定第二个参数怎么办?比如,希望创建一个偏函数cube(x),计算x3,可以用_作占位符,固定住第二个参数:

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

 

可见,创建偏函数的目的是将原函数的某些参数固定住,可以降低新函数调用的难度。

 

memoize

如果一个函数调用开销很大,我们就可能希望能把结果缓存下来,以便后续调用时直接获得结果。举个例子,计算阶乘就比较耗时:

  1. 'use strict';
  2. function factorial(n) {
  3. console.log('start calculate ' + n + '!...');
  4. var s = 1, i = n;
  5. while (i > 1) {
  6. s = s * i;
  7. i --;
  8. }
  9. console.log(n + '! = ' + s);
  10. return s;
  11. }
  12. factorial(10); // 3628800
  13. // 注意控制台输出:
  14. // start calculate 10!...
  15. // 10! = 3628800

 

用memoize()就可以自动缓存函数计算的结果:

  1. 'use strict';
  2. var factorial = _.memoize(function(n) {
  3. console.log('start calculate ' + n + '!...');
  4. var s = 1, i = n;
  5. while (i > 1) {
  6. s = s * i;
  7. i --;
  8. }
  9. console.log(n + '! = ' + s);
  10. return s;
  11. });
  12. // 第一次调用:
  13. factorial(10); // 3628800
  14. // 注意控制台输出:
  15. // start calculate 10!...
  16. // 10! = 3628800
  17. // 第二次调用:
  18. factorial(10); // 3628800
  19. // 控制台没有输出

 

对于相同的调用,比如连续两次调用factorial(10),第二次调用并没有计算,而是直接返回上次计算后缓存的结果。不过,当你计算factorial(9)的时候,仍然会重新计算。

可以对factorial()进行改进,让其递归调用:

  1. 'use strict';
  2. var factorial = _.memoize(function(n) {
  3. console.log('start calculate ' + n + '!...');
  4. if (n < 2) {
  5. return 1;
  6. }
  7. return n * factorial(n - 1);
  8. });
  9. factorial(10); // 3628800
  10. // 输出结果说明factorial(1)~factorial(10)都已经缓存了:
  11. // start calculate 10!...
  12. // start calculate 9!...
  13. // start calculate 8!...
  14. // start calculate 7!...
  15. // start calculate 6!...
  16. // start calculate 5!...
  17. // start calculate 4!...
  18. // start calculate 3!...
  19. // start calculate 2!...
  20. // start calculate 1!...
  21. factorial(9); // 362880
  22. // console无输出

 

once

顾名思义,once()保证某个函数执行且仅执行一次。如果你有一个方法叫register(),用户在页面上点两个按钮的任何一个都可以执行的话,就可以用once()保证函数仅调用一次,无论用户点击多少次:

  1. 'use strict';
  2. var register = _.once(function () {
  3. alert('Register ok!');
  4. });
  5. // 测试效果:
  6. register();
  7. register();
  8. register();

 

delay

delay()可以让一个函数延迟执行,效果和setTimeout()是一样的,但是代码明显简单了:

  1. 'use strict';
  2. // 2秒后调用alert():
  3. _.delay(alert, 2000);

 

如果要延迟调用的函数有参数,把参数也传进去:

  1. 'use strict';
  2. var log = _.bind(console.log, console);
  3. _.delay(log, 2000, 'Hello,', 'world!');
  4. // 2秒后打印'Hello, world!':

 

更多完整的函数请参考underscore的文档:http://underscorejs.org/#functions

 

JavaScript Functions

付杰
  • ¥ 1999.9元
  • 市场价:20000元
  • ¥ 388.0元
  • 市场价:388.0元
  • ¥ 298.0元
  • 市场价:398.0元
  • ¥ 58.0元
  • 市场价:58.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: