Talking about javascript functional programming and javascript Functions

Source: Internet
Author: User

Talking about javascript functional programming and javascript Functions

Function programming is a type of programming paradigm.

1 function is the first citizenCan return values or be used as parameters of other functions.

// Console is a function con (v) {console. log (v)} // execute is also a function execute (fn) {fn (1)} // pass the con function as a parameter to the execute function execute (con) // 1

2. Writing Method close to natural language

Xiao Chi eats the meal and then takes a bath, which can be shown as eat (). bathe ()

// Function eat (eat) {this. e = eat; return this;} // function of the bath function bathe (bathe) {this. B = bathe; return this;} var person = eat ("Xiao chi is eating "). bathe ("Xiao Chi has taken a bath"); console. log (person. e) // Xiao chi is in the dining console. log (person. b) // Xiao Chi has taken a bath.

3 features of functional programming

Anonymous functions, that is, functions without names, are common in functional programming. Sometimes we need to use them (non-reusable functions) to complete some functions, the following describes how to define an each function:

// Function each (arr, func) {var length = arr. length; for (var I = 0; I <length; I ++) {func (I, arr [I])} // execute the each function, upload an anonymous function as the function parameter each ([1, 2, 3], function (I, v) {console. log ('key: '+ I +', value: '+ v) ;}); // output content // key: 0, value: 1 // key: 1, value: 2 // key: 2, value: 3

Kerihua: kerihua converts a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function, and return the technology of the new function that accepts the remaining parameters and returns the result.

// Define the add function and return a function add (num) {return function (x) {return num + x ;}} add1 = add (1) console. log (add1 (3) // 4

High-order functions: a function is a high-order function that is returned as a parameter or function. The above each function is a type of high-order function.

Conclusion

In actual applications, it is not limited to functional or object-oriented methods. Usually, they are used in combination. In fact, many mainstream object-oriented languages are constantly improving themselves, for example, some features of functional programming languages are added. In JavaScript, the two are well combined. The code can be very simple, elegant, and easier to debug.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.