Let's pretend that we are going to complete a task now: write JavaScript code as much as possible according to the principles of functional languages.
The next series of articles is to let you and me start this journey. First, we need to correct some of the concepts of functional language errors that may be in your mind.
Functional formulas in JS language are seriously misunderstood.
Obviously, there are quite a lot of developers who want to use JavaScript's functional paradigm every day. I would say there is a larger portion of JavaScript developers who don't really understand these things.
I think the reason for this is that most development languages used on the web server are derived from C, and everyone knows that these languages are not functional languages.
There are generally two levels of confusion. The first layer is about the usage in the following example, which is a very common usage in jQuery:
$(".signup").click(function(event){ $("#signupModal").show(); event.preventDefault();});In the above code: We pass an anonymous function as a parameter, which is the infamous callback function in JavaScript. Will these functions be called? Not at all!
The above example illustrates a key feature of a functional language: a function is a parameter. On the other hand, this example violates almost all functional programming paradigms that can be violated using only three lines of code.
The second level of doubt is more subtle. Check out this: Only a few fashionable JS developers look at themselves like this:
Well, that's really good! But I already have a complete understanding of functional programming, and I use Underscore.js in all my projects.
Underscore.js is a very famous and ubiquitous JavaScript library. For example: Suppose I have a set of words and I need the first two letters for each word. This is very simple to write with Underscore.js:
var firstTwoLetters = function(words){ return _.map(words,function(word){ return _.first(word,2); });};Look at this JavaScript example. I used the exquisite and practical functions in the function formula: _.map and _.first. What do you think about this?
Although functions like underscore.js and _.map are both useful functional paradigms, putting these things together like this in this example still makes me a little difficult to understand. Do we really need to do this?
If we start thinking about how to be more "functional", maybe we can rewrite the above example like this:
// ...a little bit of magicvar firstTwoLetters = map(first(2));
If you think about it carefully, this line of code has the same effect as the above 5 lines. Many words are just parameters or placeholders. The real logic is to combine map function, first function and constant 2 in a meaningful way.
Some people may be wondering: What is the magic in this example? After all, it's a bit like bragging to say that any example is magical, right?
I am planning to use the next two articles to explain what the above example is magical. If you are curious, you can continue reading.
This series of blogs is to help you how to apply the beauty of functional programming to your JavaScript code.
In the next section, I will discuss various elements of functional and non-functional in the JavaScript language. With this knowledge, we can slowly form a complete system of functional programming in our minds by integrating these elements together and understand their performance in JavaScript.