javascript功能_功能性JavaScript简介
javascript功能
Hey everybody! I’ve written a book called Discover Functional JavaScript, and it’s now ready in both paperback and Kindle formats.
大家好! 我已经写了一本书《 发现功能JavaScript》 ,现在已经可以使用平装本和Kindle格式。
After publishing several articles on Functional Programming in JavaScript, at some point I realized I have enough material to think about a book. So, I started from my previous writings, filled in the missing parts and created a book on Functional Programming in JavaScript.
在发表了几篇有关使用JavaScript进行函数式编程的文章之后,在某个时候,我意识到我有足够的材料来考虑一本书。 因此,我从以前的著作开始,填补了遗漏的部分,并撰写了一本有关JavaScript函数编程的书。
What I tried to do in this book was to give practical examples of the core functional concepts. I think that if we master the fundamentals then it will be easier to handle more complex situations. And this is what this book is for.
我在本书中尝试做的是给出核心功能概念的实际示例。 我认为,如果我们掌握了基础知识,那么处理更复杂的情况将更加容易。 这就是这本书的目的。
I looked into a deeper understanding of pure functions other than that they are great. If they are so good, why don’t we write the whole application using only pure functions?
我对纯函数感到更深刻的理解是,它们不是很棒的。 如果它们是如此出色,为什么我们不只使用纯函数来编写整个应用程序呢?
The other reason behind the book is to emphasize the new way of building encapsulated objects without classes and prototypes in JavaScript. I even saw classes presented as a way to bring encapsulation to objects. Encapsulation means hiding information. Objects built with classes in JavaScript are built over the prototype system. All their properties are public, they are not encapsulated.
本书背后的另一个原因是强调了在JavaScript中没有类和原型的情况下构建封装对象的新方法。 我什至看到类作为将封装引入对象的一种方式。 封装意味着隐藏信息。 用JavaScript中的类构建的对象是在原型系统上构建的。 它们的所有属性都是公共的,没有封装。
I tried and hope I have succeeded to present the fundamental functional programming concepts in an easy to learn and practical manner. After reading the book you will understand better concepts like first-class functions, closures, currying, and partial application. You will understand what pure functions are and how to create them. You will better understand immutability and how it can be achieved in JavaScript.
我尝试并希望我已经以一种易于学习和实用的方式成功介绍了基本的函数式编程概念。 阅读本书后,您将了解更好的概念,例如一流的函数,闭包,currying和部分应用程序。 您将了解什么是纯函数以及如何创建它们。 您将更好地了解不变性以及如何在JavaScript中实现不变性。
Another thing not taken so much into account is naming. With the rise of arrow functions, more and more anonymous functions are created. The pretext behind all this is the fact that arrow functions have no this
and have a shorter syntax. I don’t challenge that, I just challenge the fact that meaningful names are what we understand best. Removing that will make code harder to understand.
另一个没有被充分考虑的事情是命名。 随着箭头功能的兴起,越来越多的匿名功能被创建。 所有这些背后的借口是,箭头函数没有this
功能,语法较短。 我没有挑战,我只是挑战一个事实,即有意义的名字是我们最了解的。 删除该代码将使代码更难理解。
The book is pretty condensed, so you can even read it a few times. In regard to the core JavaScript concepts, it aims to make an overview of them, not to enter into great detail. There are a lot of resources for that.
这本书简明扼要,因此您甚至可以阅读几次。 关于JavaScript的核心概念,它的目的是对它们进行概述,而不是详细介绍。 有很多资源。
For me, it was a great experience trying to organize my thoughts to express these ideas in a simple, practical way. I tried to focus on the main practical concepts and just eliminate everything that ads no value to the reader.
对我来说,这是一次很棒的经历,试图组织我的思想以一种简单,实用的方式表达这些思想。 我尝试着眼于主要的实用概念,并消除了那些对读者毫无价值的广告。
A deeper understanding of the fundamental concepts in JavaScript makes us better at resolving complex problems. I hope you will like it.
对JavaScript基本概念的更深入了解使我们能够更好地解决复杂的问题。 我希望你会喜欢。
Here is what you can find inside:
您可以在里面找到以下内容:
第1章:JavaScript的简要概述 (Chapter 1: A brief overview of JavaScript)
JavaScript has primitives, objects and functions. All of them are values. All are treated as objects, even primitives.
JavaScript具有原语,对象和功能。 它们都是价值。 所有这些都被视为对象,甚至是基元。
Number, boolean, string, undefined
and null
are primitives.
数字,布尔值,字符串, undefined
和null
是基元。
Variables can be defined using var
, let
and const
. The let
declaration has a block scope.
可以使用var
, let
和const
定义变量。 let
声明具有块范围。
Primitives, except null
and undefined
, are treated like objects, in the sense that they have methods but they are not objects.
从null
,它们具有方法,但不是对象,因此将null
和undefined
除外的基元视为对象。
Arrays are indexed collections of values. Each value is an element. Elements are ordered and accessed by their index number.
数组是值的索引集合。 每个值都是一个元素。 元素按其索引号排序和访问。
JavaScript has dynamic typing. Values have types, variables do not. Types can change at run time.
JavaScript具有动态类型。 值有类型,变量没有。 类型可以在运行时更改。
The main JavaScript runtime is single threaded. Two functions can’t run at the same time.
JavaScript的主要运行时是单线程的。 两个功能不能同时运行。
第2章:ES6 +中的新功能 (Chapter 2: New features in ES6+)
ES6 brings more features to the JavaScript language. Some new syntax allows you to write code in a more expressive way, some features complete the functional programming toolbox, and some features are questionable.
ES6为JavaScript语言带来了更多功能。 一些新的语法允许您以更具表现力的方式编写代码,一些功能完善了功能性编程工具箱,而某些功能则值得怀疑。
let
declaration has block scope.
let
声明具有块范围。
function doTask(){ let x = 1; { let x = 2; }console.log(x);
}
doTask(); //1
var
declaration has function scope. It doesn't have block scope.
var
声明具有功能范围。 它没有块范围。
function doTask(){ var x = 1; { var x = 2; }console.log(x);
}
doTask(); //2
第三章:一流的功能 (Chapter 3: First-class functions)
Functions are first-class objects. Functions can be stored in variables, objects or arrays, passed as arguments to other functions or returned from functions.
函数是一流的对象。 函数可以存储在变量,对象或数组中,可以作为参数传递给其他函数,也可以从函数返回。
A higher-order function is a function that takes another function as an input, returns a function, or does both.
高阶函数是将另一个函数作为输入,返回一个函数或同时执行两个函数的函数。
map()
transforms a list of values to another list of values using a mapping function.
map()
使用映射函数将值列表转换为另一个值列表。
let numbers = [1,2,3,4,5];function doubleNo(x){const result = x*2;console.log(`${x} -> ${result}`)return result;
}const doubleNumbers = numbers.map(doubleNo);
//1 -> 2
//2 -> 4
//3 -> 6
//4 -> 8
//5 -> 10
//[2, 4, 6, 8, 10]
第4章:闭包 (Chapter 4: Closures)
A closure is an inner function that has access to the outer scope, even after the outer scope container has executed.
闭包是一个内部函数,即使在执行外部作用域容器之后,也可以访问外部作用域。
The count()
function in the next example is a closure:
下一个示例中的count()
函数是一个闭包:
const count = (function(){let state = 0;return function(){state = state + 1;return state;}
})();count(); //1
count(); //2
count(); //3
第5章:函数装饰器 (Chapter 5: Function decorators)
A function decorator is a higher-order function that takes one function as an argument and returns another function, and the returned function is a variation of the argument function — Reginald Braithwaite, author of Javascript Allongé
函数装饰器是一个高阶函数,它以一个函数作为参数并返回另一个函数,返回的函数是参数函数的变体— Reginald Braithwaite, JavascriptAllongé的作者
The unary()
decorator returns a new version of the function that accepts only one argument. It may be used to fix problems when the function is called with more arguments than we need.
unary()
装饰器返回仅接受一个参数的函数的新版本。 当用比我们需要更多的参数调用函数时,它可以用来解决问题。
function unary(fn){return function(first){return fn(first);}
}const numbers = ['1','2','3','4','5','6'];
numbers.map(parseInt);
//[1, NaN, NaN, NaN, NaN, NaN]numbers.map(unary(parseInt));
//[1, 2, 3, 4, 5, 6]
第六章:纯函数 (Chapter 6: Pure functions)
A pure function is a function that, given the same input, always returns the same output and has no side effects.
纯函数是在给定相同输入的情况下始终返回相同输出且没有副作用的函数。
You may have seen examples of pure functions like the ones below and want to look at some practical examples of pure functions.
您可能已经看到了诸如以下纯函数的示例,并且想看一些纯函数的实际示例。
function double(x){return x * 2;
}function add(a, b){return a + b;
}function multiply(a, b) {return a * b;
}
Like other programming paradigms, Pure Functional Programming promises to make code easier to read, understand, test, debug, and compose. Can it deliver its promise? If it can, can we build an application using only pure functions? These are questions this chapter tries to answer.
与其他编程范例一样,纯函数式编程有望使代码更易于阅读,理解,测试,调试和编写。 能兑现诺言吗? 如果可以,我们可以仅使用纯函数来构建应用程序吗? 这些是本章试图回答的问题。
第7章不变性 (Chapter 7: Immutability)
An immutable value is a value that, once created, cannot be changed.
不变值是一旦创建就无法更改的值。
Does immutability have to do with variables that cannot change or values that cannot change? And how can we make that happen? Why do we even care about that? This chapter tries to answers these questions.
不变性与不可更改的变量或不可更改的值有关吗? 我们如何实现这一目标? 我们为什么还要在乎呢? 本章试图回答这些问题。
第8章:部分应用和curring (Chapter 8: Partial application and currying)
Partial application refers to the process of fixing a number of parameters by creating a new function with fewer parameters than the original.
部分申请 指通过创建参数比原始参数少的新函数来固定多个参数的过程。
Currying is the process of transforming a function with many parameters into a series of functions that each takes a single parameter.
咖喱化是将具有多个参数的函数转换为一系列函数的过程,每个函数都具有一个参数。
Usually we find examples using currying to add or multiply a few numbers, like in the code below:
通常,我们会找到一些示例,这些示例使用currying将几个数字相加或相乘,如下面的代码所示:
function add(a) {return function(b){return function(c){return a + b + c;}}
}add(1)(2)(3);
//6
Does currying have a practical application? This chapter shows some practical examples of using partial application and currying.
currying有实际应用吗? 本章显示了一些使用部分应用程序和currying的实际示例。
第9章:函数组成 (Chapter 9: Function composition)
Function composition is applying one function to the result of another.
函数组成是将一个函数应用于另一个函数的结果。
function compose(...functions){return function(x){return functions.reduceRight((value, f) => f(value), x);}
}f(g(x)) === compose(f,g)(x);
第10章:意图揭示名称 (Chapter 10: Intention revealing names)
Functions can be created with or without a name. The arrow syntax usually creates anonymous functions.
可以使用或不使用名称来创建函数。 箭头语法通常创建匿名函数。
(() => {/*code*/(() => {/*code*/})();
})();
Anonymous functions appear as “(anonymous)” in the CallStack.
匿名函数在CallStack中显示为“(匿名)”。
Intention revealing names improve code readability.
意图揭示名称可以提高代码的可读性。
第11章:使代码更易于阅读 (Chapter 11: Making code easier to read)
This chapter shows examples of refactoring imperative code with functional programming techniques and looks at the readability of the final code.
本章显示了使用函数式编程技术重构命令性代码的示例,并介绍了最终代码的可读性。
第十二章:异步编程 (Chapter 12: Asynchronous programming)
In an application, there are two kinds of functions: synchronous and asynchronous. We take a look at the asynchronous programming model in JavaScript.
在应用程序中,有两种功能:同步和异步。 我们来看一下JavaScript中的异步编程模型。
第十三章:带有原型的对象 (Chapter 13: Objects with prototypes)
Objects are dynamic collections of properties, with a “hidden” property to the object’s prototype.
对象是属性的动态集合,对象的原型具有“隐藏”属性。
Objects inherit from other objects.
对象从其他对象继承。
class
is a sugar syntax from creating objects with a custom prototype.
class
是使用自定义原型创建对象的糖语法。
class Counter {constructor(){this.state = 0;}increment(){this.state = this.state + 1;return this.state;}decrement(){this.state = this.state - 1;return this.state;}
}const counter = new Counter();
counter.increment(); //1
counter.increment(); //2
counter.increment(); //3
counter.decrement(); //2
第14章:带有闭包的对象 (Chapter 14: Objects with closures)
With closures we can create encapsulated and flexible objects. Consider the same counter object created with closures:
使用闭包,我们可以创建封装的灵活对象。 考虑使用闭包创建的相同计数器对象:
function Counter() {let state = 0;function increment(){state = state + 1;return state;}function decrement(){state = state - 1;return state;}return Object.freeze({increment, decrement})
}const counter = Counter();
counter.increment(); //1
counter.increment(); //2
counter.increment(); //3
counter.decrement(); //2
This chapter presents more encapsulated objects and discusses the difference between objects built with closures and prototypes.
本章介绍了更多封装的对象,并讨论了使用闭包和原型构建的对象之间的区别。
第15章:方法装饰器 (Chapter 15: Method decorators)
Method decorators are a tool for reusing common logic.
方法装饰器是用于重用通用逻辑的工具。
第16章:等待新的编程范例 (Chapter 16: Waiting for the new programming paradigm)
The last chapter contains thoughts on Functional and Object Oriented Programming in JavaScript.
上一章包含有关JavaScript中的功能和面向对象编程的思想。
Enjoy the book!
享受这本书 !
You can find me on Twitter.
您可以在Twitter上找到我。
翻译自: https://www.freecodecamp.org/news/an-introduction-to-functional-javascript-e8dab63bb51d/
javascript功能
相关文章:

样式集,小程序群聊,聊天室样式,效果图
效果图 注:(码云 group_chat_yun ) 代码: <!-- <view class"top"><image class"page_editright" catchtap"navBack" mode"widthFix" src"/images/editright.png&quo…

GeoQuiz项目的开发与总结2
时间过得很快,第二阶段的学习结束了。 本周的主要工作是完成了Geoquiz项目的剩余部分。 首先是学到了些什么,最主要的是工作的流程,然后是界面的布局,菜单栏的设计到等。当然我觉得我学到的应该是工作制作的思维方式吧。 再来说说…

【12.16】VC++调用Word OLE进行自动化生成报表
! 12-16更新 初始化博客转载于:https://www.cnblogs.com/miki-52/p/5052689.html

python timber_如何使用Timber更有效地记录日志
python timberby Ayusch Jain通过Ayusch Jain 如何使用Timber更有效地记录日志 (How to log more efficiently with Timber) Logging is one of the most used utilities in the Android framework. It is really helpful in debugging your code when debugging by break-poi…

node 实现blog博客
https://cnodejs.org/topic/581b0c4ebb9452c9052e7acb转载于:https://www.cnblogs.com/zhangshuda/p/7640363.html

小程序输入框导致界面上移,在输入的时候固定住页面的解决代码
效果: 代码: <view class"comment" style"bottom:{{bottom}}px"><view class"emoji_block" wx:if{{emoji_block_show}}><view wx:for{{connectemoji}} catchtap"add_biaoqing" id"{{item}}…

react中纯函数_如何在纯React中创建电子邮件芯片
react中纯函数by Andreas Remdt由Andreas Remdt 如何在纯React中创建电子邮件芯片 (How to create email chips in pure React) Imagine that you, the good-looking developer (yes, I’m talking to you!), want to build an invitation form where users can add one or mo…
servlet程序HTTP Status 500 - Error instantiating servlet class 解决
一、项目存放路径问题(最常见) 在安装Tomcat时,运行程序都正常,但却打不开http://localhost:8080/,在Tomcat目录webapps下也找不到自己做的项目,这时因为你做的项目没有部署到webapps目录下,倒霉的时候就会遇到报错HTT…

ASP.NET将原始图片按照指定尺寸等比例缩放显示图片
网站上可能会有很多图片,比如产品图片等,而且他们可能大小不一,宽度和高度也不一定一样,有的很大有的很小。如果放在一张网页上,可能会破坏版面,但是如果强制让他们按照指定的宽度和高度显示,因…

前端开发框架选择
Vue Vant 适用场景:开发移动端 (vue) 上手难度:1 Vant是一款很好用的移动端UI框架,非常轻便,适合小型项目 https://vant-contrib.gitee.io/vant/#/zh-CN/ 微信小程序 适用场景:微信小程序(小程序原生框架…

anki_Anki如何挽救我的工程生涯
ankiby Jeffrey Shek通过Jeffrey Shek Anki如何挽救我的工程生涯 (How Anki saved my Engineering Career) I was burned out and my software career was stalling just three years in. My memory sucked. Was my poor memory from stress, lack of sleep or was it always …

信息安全系统设计基础期末总结
【博客汇总】 一、每周读书笔记链接汇总 •[第二周读书笔记] http://www.cnblogs.com/20135302wei/p/4842480.html •[第三周读书笔记] http://www.cnblogs.com/20135302wei/p/4858760.html •[第四周读书笔记] http://www.cnblogs.com/20135302wei/p/4870113.html •[第五周读…

方法 retrun 异步的值,创建一个变量直接等于一个异步方法返回的值
需求:我想创建一个变量,他的值是一个openid, openid 从 getOpenid (封装的一个异步方法) 里面返回,通常调用 getOpenid ,会返回一个Promise 对象,.then 之后才能得到值,例如: //模拟一个异步方…

ps混合模式glsl代码
https://github.com/jamieowen/glsl-blend 转载于:https://www.cnblogs.com/guochen/p/7645227.html

应用程序启动器 标记为信任_为什么您今天不能信任应用程序-以及如何解决它...
应用程序启动器 标记为信任亲爱的每家高科技公司,请窃取这个想法 (Dear Every Single Tech Company, Please Steal This Idea) When you send a photo to someone, your messaging app actually first sends the photo to an app’s server, which then sends the p…

小程序客服自动回复图片,云开发的实现
小程序先开通云开发,创建一个文件夹,并配置为云函数的目录 小程序客服自动回复图片实现步骤: 1.云函数接收消息推送 第一步:开发者工具云开发控制台中增加配置 打开云开发控制台,到设置 tab 中选择全局设置 - 添加消…

ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决
方法1: a、通过oracle 工具 Net Configuration Assistant 重新配置监听,注意如果有双网卡请配置第一块网上IP。如何知道哪一块是一块网卡,可以通过ipconfig命令查看最上面的ip就是一块网卡的 b、通过Net Configuration Assistant 工具配置 “…

r语言 编辑 d3.js_d3.js的语言介绍
r语言 编辑 d3.jsby Matt Oxley由马特奥克斯利(Matt Oxley) d3.js的语言介绍 (A linguistic introduction to d3.js) 如何从一个试探者变成一个自信的用户 (How to go from a tentative to a confident user) d3.js is a fantastic library — but I can honestly tell you th…

【Luogu3041】视频游戏的连击(AC自动机,动态规划)
题面链接 题解 首先构建出AC自动机 然后在AC自动机上面跑DP 转移很显然从Trie树的节点跳到他的儿子节点 但是要注意一个问题, 在计算的时候,每一个节点加入后能够 造成的贡献 要加上他的子串的贡献 至于DP: 设f[i][j]表示已经使用了i个字母 当…

拥抱高效、拥抱 Bugtags 之来自用户的声音(三)
小编按:这是一篇 Bugtags 用户来稿,主要是介绍了使用 Bugtags 前后对测试及解决 Bug 所带来的变化,感谢山西农业大学 - 高正炎同学对 Bugtags 的信赖和支持。小编在这里诚邀各位热心用户向我们投稿,说出你使用 Bugtags 的故事。 0…

小程序打开文档标题乱码处理
先下载,自定义临时文件目录名称,然后再打开就可以了。 wx.downloadFile({url: https://xxx.cn/sfxy.docx, //仅为示例,并非真实的资源filePath: wx.env.USER_DATA_PATH /这是自定义标题.docx,success(res) {console.log(0, res)wx.openDocum…

我是“真正的”软件工程师吗?
by Sun-Li Beatteay通过孙丽贝蒂 我是“真正的”软件工程师吗? (Am I a “real” Software Engineer yet?) Am I a “real” Software Engineer yet?我是“真正的”软件工程师吗? This question has haunted me for years. And it seems I’m not al…

ntpdate[31915]: the NTP socket is in use, exiting
[rootmaster local]# ntpdate cn.pool.ntp.org 10 Oct 13:24:36 ntpdate[31915]: the NTP socket is in use, exitingcron 作业中运行 ntpdate,以便大约每隔一小时就设置一次本地时间。最近,我每次运行该命令时都会收到下列错误消息。 ntpdate[31915]: t…

小程序云开发更新数组的指定对象的值
云开发,在小程序实现 代码说明‘: 在这里,数据集合 groupList 中的 userList 是一个用户列表数组,我要更新数组中,openid 等于我的openid 的在线状态为 true。 先查询条件,集合里面的 _id 等于我传的id&a…
Unreal Engine 4 RenderTarget制作Live Camera效果
Unreal Engine 4 RenderTarget制作Live Camera效果 先上效果: Live Camera我不知道怎么翻译。反正意思就是将一个摄影机的Image渲染到一个2D平面上。 以下介绍下详细的实现方法: 1.创建一个Scene Capture 2D对象 将这个对象拖动到合适的地方。2.创建Re…

领导让我重构代码_领导不是由代码构成
领导让我重构代码The team leader is a key figure in a team of developers. It is a difficult role, involving both technical and social skills. This is the reason why not everyone is tailored for it.团队负责人是开发人员团队中的关键人物。 这是一项艰巨的任务&am…

Spring学习-理解IOC和依赖注入
最近刚买了一本介绍ssm框架的书,里面主要对Mybatis、spring、springmvc和redis做了很多的讲解,个人觉得虽然有的内容我看不懂,但是整体上还是不错的。最近正在学习中,一边学习一边做一些总结,现在我对这些思想技术还没…

windows server2012怎样关机怎样重启-详细教程
|浏览:1991|更新:2014-12-15 17:33123456分步阅读百度经验:jingyan.baidu.com windows server2012和以往有些不同,关机/重启按钮不是在左边,甚至左边的“开始”都不见了,那怎样关机/重启呢?这里开始演示&am…

封装 localStorage 缓存,兼容网页,微信小程序,uni-app
封装的缓存功能,兼容网页,微信小程序,uni-app 使用,支持设置缓存,获取缓存,移除缓存,清空缓存,设置缓存时间,分组缓存设置。 把最下面的 Str4.js 代码拷贝到项目内可以直…

考csp所需算法_CSP vs RxJS:您所不知道的。
考csp所需算法by Kevin Ghadyani通过凯文加迪亚尼(Kevin Ghadyani) CSP vs RxJS:您所不知道的。 (CSP vs RxJS: what you don’t know.) CSP发生了什么? (What happened to CSP?) You probably clicked this article thinking “what is CSP?” It’s…