作家
登录

JavaScript的函数式编程,你了解吗?

作者: 来源: 2017-10-26 09:46:30 阅读 我要评论

);  // This will go in the first slot. 
  • const result = giveMe1('Turtle Doves');               // Finally fill in the second argument. 
  • console.log(result); 
  • // 1: Partridge in a Pear Tree 
  • // 2: Turtle Doves 
  • // 3: French Hens 
  • 在我们停止商量柯里化之前最后的议题是偏函数应用partial application。偏函数应用和柯里化经常同时出场,尽管它们实际上是不合的概念。一个柯里化的函数照样柯里化的函数,即使没有给它任何参数。偏函数应用,另一方面是仅仅给一个函数传递部分参数而不是所有参数。柯里化是偏函数应用常用的办法之一,然则不是独一的。

    JavaScript 拥有一个内置机制可以不依附柯里化来做偏函数应用。那就是 function.prototype.bind 办法。这个办法的一个特别之处在于,它请求你将 this 作为第一个参数传入。 如不雅你一向行面向对象编程,那么你可以经由过程传入 null 来忽视 this。

    1. 1function giveMe3(item1, item2, item3) { 
    2.   return ` 
    3.     1: ${item1} 
    4.     2: ${item2} 
    5.     3: ${item3} 
    6.   `; 
    7. const giveMe2 = giveMe3.bind(null'rock'); 
    8. const giveMe1 = giveMe2.bind(null'paper'); 
    9. const result = giveMe1('scissors'); 
    10. console.log(result); 
    11. // 1: rock 
    12. // 2: paper 
    13. // 3: scissors 

    总结

    1. const vehicles = [ 
    2.   { make: 'Honda', model: 'CR-V', type: 'suv', price: 24045 }, 
    3.   { make: 'Honda', model: 'Accord', type: 'sedan', price: 22455 }, 
    4.   { make: 'Mazda', model: 'Mazda 6', type: 'sedan', price: 24195 }, 
    5.   { make: 'Mazda', model: 'CX-9', type: 'suv', price: 31520 }, 
    6.   { make: 'Toyota', model: '4Runner', type: 'suv', price: 34210 }, 
    7.   { make: 'Toyota', model: 'Sequoia', type: 'suv', price: 45560 }, 
    8.   { make: 'Toyota', model: 'Tacoma', type: 'truck', price: 24320 }, 
    9.   { make: 'Ford', model: 

        推荐阅读

        如何处理JavaScript内存泄露

      沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 几周前,我们开端写一个系列,>>>详细阅读


      本文标题:JavaScript的函数式编程,你了解吗?

      地址:http://www.17bianji.com/lsqh/38230.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)