Home Web Front-end JS Tutorial A brief discussion on javascript functional programming_javascript skills

A brief discussion on javascript functional programming_javascript skills

May 16, 2016 pm 03:40 PM
javascript functional programming

Functional programming is a programming paradigm

1 The function is the first citizen, it can return a value and can also be used as a parameter of other functions

//console是一个函数
function con(v){
 console.log(v)
}
// execute 也是一个函数
function execute(fn){
 fn(1)
}
//将con函数作为参数传进execute函数
execute(con) // 1

Copy after login

2 Writing method close to natural language

Xiaochi finished eating and then went to take a bath. It can be expressed as eat().bathe()

// 吃饭函数
function eat(eat){
 this.e = eat;
 return this;
}
// 洗澡函数
function bathe(bathe){
 this.b = bathe;
 return this;
}

var person = eat("晓池在吃饭").bathe("晓池去洗澡了");
console.log(person.e) // 晓池在吃饭
console.log(person.b) // 晓池去洗澡了

Copy after login

3 Features of Functional Programming

Anonymous functions, that is, functions without names, are very common in functional programming. Sometimes we need to use them (unreused functions) to complete some functions. Let’s learn about it by defining an each function:

// 自定义each函数
function each(arr,func){
 var length = arr.length;
 for(var i = 0 ;i <length; i++){
  func(i,arr[i])
 }
}
// 执行each函数,传进一个匿名函数作为该函数的参数
each([1,2,3],function(i,v){
 console.log('key:' + i + ',value:' +v);
});
//输出内容
//key:0,value:1
//key:1,value:2
//key:2,value:3

Copy after login

Currying: Currying is the technique of transforming a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a new function that accepts the remaining parameters and returns the result.

//定义add函数,并返回一个函数
function add(num){
 return function(x){
   return num + x;
 }
}
add1 = add(1)
console.log(add1(3)) // 4

Copy after login

Higher-order function: If there is a function as a parameter or a function returns a function internally, the function can be called a higher-order function. The above each function is considered a type of high-order function.

Conclusion

In actual applications, it is not limited to functional or object-oriented, and is usually a mixture of the two. In fact, many mainstream object-oriented languages ​​​​are constantly improving themselves, such as adding some features of functional programming languages. Wait, in JavaScript, the two are well combined. The code can not only be very simple and beautiful, but also easier to debug.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Python Lambda expressions: abbreviated, concise, powerful Python Lambda expressions: abbreviated, concise, powerful Feb 19, 2024 pm 08:10 PM

pythonLambda expressions are a powerful and flexible tool for creating concise, readable, and easy-to-use code. They are great for quickly creating anonymous functions that can be passed as arguments to other functions or stored in variables. The basic syntax of a Lambda expression is as follows: lambdaarguments:expression For example, the following Lambda expression adds two numbers: lambdax,y:x+y This Lambda expression can be passed to another function as an argument as follows: defsum( x,y):returnx+yresult=sum(lambdax,y:x+y,1,2)In this example

What are the benefits of using C++ lambda expressions for functional programming? What are the benefits of using C++ lambda expressions for functional programming? Apr 17, 2024 am 10:18 AM

C++ lambda expressions bring advantages to functional programming, including: Simplicity: Anonymous inline functions improve code readability. Code reuse: Lambda expressions can be passed or stored to facilitate code reuse. Encapsulation: Provides a way to encapsulate a piece of code without creating a separate function. Practical case: filtering odd numbers in the list. Calculate the sum of elements in a list. Lambda expressions achieve the simplicity, reusability, and encapsulation of functional programming.

Common mistakes and pitfalls of golang functional programming Common mistakes and pitfalls of golang functional programming Apr 30, 2024 pm 12:36 PM

There are five common mistakes and pitfalls to be aware of when using functional programming in Go: Avoid accidental modification of references and ensure that newly created variables are returned. To resolve concurrency issues, use synchronization mechanisms or avoid capturing external mutable state. Use partial functionalization sparingly to improve code readability and maintainability. Always handle errors in functions to ensure the robustness of your application. Consider the performance impact and optimize your code using inline functions, flattened data structures, and batching of operations.

How to optimize Golang functional programs using lazy evaluation? How to optimize Golang functional programs using lazy evaluation? Apr 16, 2024 am 09:33 AM

Lazy evaluation can be implemented in Go by using lazy data structures: creating a wrapper type that encapsulates the actual value and only evaluates it when needed. Optimize the calculation of Fibonacci sequences in functional programs, deferring the calculation of intermediate values ​​until actually needed. This can eliminate unnecessary overhead and improve the performance of functional programs.

What are the advantages and disadvantages of Java functions compared to other functional programming languages? What are the advantages and disadvantages of Java functions compared to other functional programming languages? Apr 24, 2024 pm 02:51 PM

Java functional programming advantages include simplicity, composability, concurrency, test-friendliness, and performance. Disadvantages include learning curve, difficulty in debugging, limited flexibility, and performance overhead. Its key features include pure functions without side effects, data processing pipelines, stateless code, and efficient streaming APIs.

Python Lambda expressions: Uncovering the power of anonymous functions Python Lambda expressions: Uncovering the power of anonymous functions Feb 24, 2024 am 09:01 AM

Lambda expression in python is another syntax form of anonymous function. It is a small anonymous function that can be defined anywhere in the program. A lambda expression consists of a parameter list and an expression, which can be any valid Python expression. The syntax of a Lambda expression is as follows: lambdaargument_list:expression. For example, the following Lambda expression returns the sum of two numbers: lambdax,y:x+y. This Lambda expression can be passed to other functions, such as the map() function: numbers=[ 1,2,3,4,5]result=map(lambda

See all articles