楼主: Lisrelchen
1233 12

[Dan Mantyla]Functional Programming in JavaScript [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-8-21 10:37:18 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Functional Programming in JavaScript.pdf (5.84 MB)

  1. Functional Programming in JavaScript
  2. Unlock the powers of functional programming hidden within JavaScript to build smarter, cleaner, and more reliable web apps

  3. By 作者: Dan Mantyla
  4. ISBN-10 书号: 1784398225
  5. ISBN-13 书号: 9781784398224
  6. Release 出版日期: 2015-03-26
  7. pages 页数: (152)
  8. List Price: $29.99
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Functional Javascript function Program

沙发
Lisrelchen 发表于 2017-8-21 10:38:49
  1. Self-invoking functions and closures
  2. What if we could return a function expression that in-turn returns the values array? Variables declared in a function are available to any code within the function, including self-invoking functions.

  3. By using a self-invoking function, our dilemma is solved.

  4. var ValueAccumulator = function() {
  5.   var values = [];
  6.   var accumulate = function(obj) {
  7.     if (obj) {
  8.       values.push(obj.value);
  9.       return values;
  10.     }
  11.     else {
  12.       return values;
  13.     }
  14.   };
  15.   return accumulate;
  16. };

  17. //This allows us to do this:
  18. var accumulator = ValueAccumulator();
  19. accumulator(obj1);
  20. accumulator(obj2);
  21. console.log(accumulator());
  22. // Output: [obj1.value, obj2.value]
复制代码

藤椅
Lisrelchen 发表于 2017-8-21 10:40:00
  1. Higher-order functions
  2. Self-invoking functions are actually a form of higher-order functions. Higher-order functions are functions that either take another function as the input or return a function as the output.

  3. Higher-order functions are not common in traditional programming. While an imperative programmer might use a loop to iterate an array, a functional programmer would take another approach entirely. By using a higher-order function, the array can be worked on by applying that function to each item in the array to create a new array.

  4. This is the central idea of the functional programming paradigm. What higher-order functions allow is the ability to pass logic to other functions, just like objects.

  5. Functions are treated as first-class citizens in JavaScript, a distinction JavaScript shares with Scheme, Haskell, and the other classic functional languages. This may sound bizarre, but all this really means is that functions are treated as primitives, just like numbers and objects. If numbers and objects can be passed around, so can functions.

  6. To see this in action, let's use a higher-order function with our ValueAccumulator() function from the previous section:

  7. // using forEach() to iterate through an array and call a
  8. // callback function, accumulator, for each item
  9. var accumulator2 = ValueAccumulator();
  10. var objects = [obj1, obj2, obj3]; // could be huge array of objects
  11. objects.forEach(accumulator2);
  12. console.log(accumulator2());
复制代码

板凳
Lisrelchen 发表于 2017-8-21 10:41:53
  1. Method chains
  2. Chaining methods together in JavaScript is quit common. If you've used jQuery, you've likely performed this technique. It's sometimes called the "Builder Pattern".

  3. It's a technique that is used to simplify code where multiple functions are applied to an object one after another.

  4. // Instead of applying the functions one per line...
  5. arr = [1,2,3,4];
  6. arr1 = arr.reverse();
  7. arr2 = arr1.concat([5,6]);
  8. arr3 = arr2.map(Math.sqrt);
  9. // ...they can be chained together into a one-liner
  10. console.log([1,2,3,4].reverse().concat([5,6]).map(Math.sqrt));
  11. // parentheses may be used to illustrate
  12. console.log(((([1,2,3,4]).reverse()).concat([5,6])).map(Math.sqrt) );
复制代码

报纸
西门高 发表于 2017-8-21 10:48:18
谢谢分享

地板
鲜艳元素 在职认证  发表于 2017-8-21 16:58:58
不错的好书

7
kavakava 在职认证  发表于 2017-8-21 19:58:50
Thanks

8
MouJack007 发表于 2017-8-21 20:31:30
谢谢楼主分享!

9
MouJack007 发表于 2017-8-21 20:31:47

10
啸傲江弧 发表于 2017-8-22 00:00:11
Thanks for sharing!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-2 17:52