Buy one get One free: This series helps you master functional programming and offers underscore skills for bonus purposes


Daily BB

In the face of ever-changing programming languages, functional programming wars are being waged. How can flexible and ancient javascript not enter the fray? Javascript is a natural proponent of functional foundations. If you want to be a high-powered programmer whether you’re on the front end, whether you’re on the back end, whether you’re on the full stack, whether you’re on the Web or hybrid, how can you not master that? The author is mainly a Java practitioner, with deep-rooted object-oriented thought. Let’s take javascript as the cornerstone, break and then stand up, re-learn javascript, carry out functional programming, and feel the extraordinary fun of programming.


Underscore can be minimized for javascript function programming, but underscore is essential, and future articles in this series will likely make extensive use of the library, whether or not you master underscore.

The first lesson I have to say

  • call()andapply()

Mastering Call () and apply() is not the key to functional programming, but the cornerstone. Let me briefly explain that call and apply exist for only one purpose: Change the direction of this inside the function as a whole. This is not a cliche, but a waste of time.

/**
 * Created by Venda-GM on 2017/10/18.
 */

function Peaple() {}
Peaple.prototype = {
    name:'Ming',
    say:function(){
        alert(this.name);
    }
}

var Peaple1 = {
    name:'jack'} var peaple = new Peaple(); peaple.say.call(Peaple1); / / jack Bauer peaple. Say. Apply (Peaple1); / / jack / / * * * * * * * * * * * * * * * * * * knowledge point 0.0 * * * * * * * * * * * * * * * Fn1 did not say this approach, but fn prototype has, then fn. Call...Copy the code

Peaple (); Peaple (); Peaple ()

  • Conclusion:

Both Call and Apply change the function's this object

There is always a difference between the two functions

The [remaining] arguments in the call() method must be passed directly to the apply() function, which takes two arguments: a run-time scope and an array of arguments, or arguments, etcCopy the code
  • argumentsWhat is?

As with Call and appply, arguments are built-in methods for each function, and arguments are properties to retrieve all variables passed to the method. Note ①_. ToArray is commonly used in libraries.

  • With that in mind, let’s put together an example:

We make a function: it takes a function, returns a function, and executes the returned function using Apply.

        function splat(fun){ 
            return function(array){
                return fun.apply(console,array);
            }
        }
        var addArray = splat(function(x,y){
            this.log(x,y)
            returnx+y; }) addArray ([1, 2]); //3 think for 1 minute, then I will parse what happened. ! [](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2017/10/19/3147f656dc04af503316778871056470~tplv-t2oag A2asx-image.image) when we call 'addArray', addArray calls' splat() 'and passes it a' function '(let's call it' solution 'for short), and it doesn't do much good, so splat() returns a function that says: '" I can't solve it either. Your plan is good. Use yours." 'With a wave of the pen,' fun.apply() '[agree!] And return your submitted '[1,2]' to you after it is executed according to your solution. - And find that the 'this' object inside' addArray 'changes from' window 'to' console '. Somebody said is there a crane? The following exampleCopy the code
Function splat(fun){return function(array){var math_π =[1,4,1,5,9,2,6,5]; Return fun. Apply (math_ PI, array); } } var addArray = splat(function(x,y){ this.push(x+y) console.log(this); / /,4,1,5,9,2,6,5,3 [1] return x + y; }) addArray ([1, 2]); / / 3 ` ` `Copy the code

We have a private attribute math_π, we don’t want it to be global, and we want it to do something with math_π when we execute addArray’s anonymous method, so we can apply the pointer to it and do something with it

I talked about object-oriented programming in the past and said that the private object oriented approach is to make a prototype chain and set up get/set, new an object, get to it, object oriented is easy to understand, but isn’t it a little bit like Java?


What was that example about? The amnesia strikes again! That we implemented a function that took a function, that returned a function, that returned a function that executed the accepted function, and changed the scope.

We can do the opposite, if there is a need:

I have a method that only accepts arrays, but now there is a force majeure that only allows me to pass strings, I don’t know how many, what should I do?

// The original method var F =function(array){
            return array.join(' ')}Copy the code

Want to transmit data

1,2,3,4,5,7,7, z, WWW, DDDCopy the code

We first think of arguments, how can we implement the requirements as they are? Use the call! perfectly

// Make a converter var ParamsConvertor =function(fun) {
    return function(){// Return an anonymous function fun.call(this, _.toarray (arguments)); 1.}}Copy the code

Let’s call:

,2,3,4,5,7,7 ParamsConvertor (F) (1,'zzz'.'www'.'ddd');
Copy the code

Print result:

1 2 3 4 5 7 7 zzz www ddd
Copy the code

Perfect!

It’s exciting just to touch the edges of the function, but what about actually starting to program?

I’m going to stop here at the end of the day

Ps: ①_. ToArray is a function of underscore,

Toarray_.toarray (list) converts a list(any object that can be iterated over) to an array, which is useful when converting arguments objects. (function() {return_.toArray(arguments).slice(1); }) (1, 2, 3, 4); = > [2, 3, 4]Copy the code

We see that arguments objects are also used.


Underscore (lodash) underscore (lodash)

The code address

In the future, blogs will be first published in this Git library, and write a list. If you are interested, you can click “star”. Click “star” and you will not get lost.


I’m going to get into functional programming, are you ready, and I’m going to try to update the design patterns as much as I can. I was going to do a series of updates, but I’m not going to be able to do anything else. Also want to wrap a ElasticSearch client in Electron, continue to maintain the crawler framework, a lot of things to do, take your time.