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.

数字,布尔值,字符串, undefinednull是基元。

Variables can be defined using var, let and const. The let declaration has a block scope.

可以使用varletconst定义变量。 let声明具有块范围。

Primitives, except null and undefined, are treated like objects, in the sense that they have methods but they are not objects.

null ,它们具有方法,但不是对象,因此将nullundefined除外的基元视为对象。

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功能

javascript功能_功能性JavaScript简介相关推荐

  1. javascript 过滤_功能性JavaScript中的过滤和链接

    javascript 过滤 本文由Dan Prince , Vildan Softic和Joan Yinn进行了同行评审. 感谢所有SitePoint的同行评审员使SitePoint内容达到最佳状态! ...

  2. javascript功能_最新版本JavaScript仅具有2个新功能。 这是他们的工作方式。

    javascript功能 by Tiago Lopes Ferreira 由Tiago Lopes Ferreira 最新版本JavaScript仅具有2个新功能. 这是他们的工作方式. (The n ...

  3. javascript教程_最好JavaScript教程

    javascript教程 JavaScript is the most widely used scripting language on Earth. And it has the largest ...

  4. javascript 框架_克服JavaScript框架疲劳

    javascript 框架 by Tero Parviainen 通过Tero Parviainen 克服JavaScript框架疲劳 (Overcoming JavaScript Framework ...

  5. javascript原型_使用JavaScript的示例报告卡Web应用程序原型

    javascript原型 Hi! At times, beginners always find it hard getting the application of the theory they ...

  6. javascript 模板_了解JavaScript中的模板文字

    javascript 模板 The author selected the COVID-19 Relief Fund to receive a donation as part of the Writ ...

  7. javascript 注入_注入JavaScript牟利:如何检测和阻止撇取者

    javascript 注入 In 2019 British Airways was fined a remarkable £183 million for a data breach of its s ...

  8. javascript语法_了解JavaScript中的解构,剩余参数和传播语法

    javascript语法 The author selected the COVID-19 Relief Fund to receive a donation as part of the Write ...

  9. jq 移动端网页分享功能_原生javascript实现分享到朋友圈功能 支持ios和android

    现在主流的分享工具也有很多,例如JiaThis.bShare分享,甚至一些大公司的如百度分享,但是他们依旧停留在只是在PC端的分享,对手机端的支持不是太好. 大家都知道现在很多手机端浏览器都内置了一些 ...

最新文章

  1. leetcode-45 跳跃游戏II
  2. 有哪些「魔改」损失函数,曾经拯救了你的深度学习模型?
  3. mysql sql优化_浅谈mysql中sql优化
  4. 什么是 Native、Web App、Hybrid、React Native和Weex?
  5. 语言兔子繁衍问题讲解_颍湄脞録兔子不搁那窝里
  6. webpack----loader
  7. zookeeper springboot_摊牌了!我要手写一个“Spring Boot”
  8. matlab mcc 安装,matlab中安装mcc
  9. 2011年最后一小时
  10. 第一章 WebGL简介 Introduction
  11. kafka buffer.memory参数入门
  12. php中几个操作函数参数的函数func_num_args() func_get_args() func_get_arg($i)php
  13. 蓝牙耳机哪款适合玩枪战类手游?低延迟听声辨位五款蓝牙耳机推荐
  14. Excel VBA 小程序 - 文本型数字转为数值型数字
  15. Mysql索引、命令重点介绍
  16. 发送ajax请求接收json数据,ajax接收到的json数据是空的
  17. 谷歌2017面经题集
  18. HW - VCN 介绍
  19. 启动、停止elasticsearch的脚本(没有技术含量)
  20. 第 2-3 课:流式布局组件详解(Flow、Wrap)

热门文章

  1. 【jsp】兴唐第三十节课作业
  2. elasticsearch 文档操作
  3. Flutter专题1-环境搭建
  4. 微信小程序实时聊天之WebSocket
  5. swift 加载gif 框架图片
  6. 04- CoreData轻量级版本的迁移
  7. 没有学不会的C++:用户自定义的隐式类型转换
  8. Spring事务管理的底层逻辑—源码解析
  9. IDEA的常用操作(快捷键)
  10. 解决eclipse ctrl+鼠标左键不能用