search
HomeWeb Front-endJS TutorialFunction sharing that enables JavaScript to break apart

This article mainly introduces you to the use of functional programming to break away from JavaScript. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.

About DHTML

DHTML is the abbreviation of Dynamic HTML, which is dynamic HTML (an application under the Standard Universal Markup Language), which is relatively traditional static HTML. A concept of making web pages in Chinese. The so-called Dynamic HTML (DHTML for short) is not actually a new language. It is just an integration of HTML, CSS and client-side scripts, that is, a page includes html+css+javascript (or other client-side scripts). ), where css and client scripts are written directly on the page instead of linking to related files.

At that time, JavaScript evolved slowly and was mainly used in form validation. Therefore, it was not as popular as it is today and did not attract much attention. It can be said to be just an icing on the cake. You need to ensure that your application can still be used normally after JavaScript is disabled in the browser. Later, frameworks appeared one after another: jQuery, Knockout, Angular, React, Vue, etc.

Similarly, JavaScript is also evolving at an accelerated pace. We have only been using ES6 for a while, and now people have almost skipped ES7 and started talking about ES8.

And, we have many alternatives, such as TypeScript, CoffeScript, ClojureScript, ELM, etc.

We have been overwhelmed by too many frameworks and languages, and it is difficult to keep track of and master them all.

Wrong Route

As JavaScript matured, the concepts of object-oriented programming (OOP) filtered in, and I used to love it.

I started trying all the different ways to create classes, and I finally got to use inheritance correctly. I said to myself: JavaScript is starting to really look like a language!

However, it was not until many years later that I discovered that OOP is the worst design introduced by JavaScript!

I try to bring my understanding of C# to JavaScript. I was full of expectations at first, but later I found it was too complicated and brain-burning.

This is mainly because JavaScript’s prototypal inheritance is different from C#. I am used to writing elegant code like console.log(this) every day. But what now? If I accidentally don't follow the rules, it will give me nightmares. Private methods and private values ​​must have an underscore in front of their names, and even closures must be used to ensure privacy.

Therefore, not only OOP has caused many problems, but also the addition of OOP has brought many new problems.

Functional Programming

I didn’t understand it at first. I can read and understand this functional code, but I don't know why! Eventually, I forced myself to learn it.

Functional languages ​​gave me a new perspective and allowed me to look at programming in a completely different way.

It will feel unnatural at first and it will take time to adapt. All definitions are based on functions, the values ​​cannot be changed, and there is no state. I use functional thinking to solve problems. Because I was unfamiliar with it, it took me longer to learn. Gradually, I became proficient in using functional methods to program. And, I also know the underlying meaning of all code written this way.

My code is more concise and easy to reuse. Gradually, the language features I used to use disappeared from the code, and my code looked like it was written in another language. Am I still using JavaScript?

1. No longer use var

I replaced var with const. Through functional design, my functions are pure. The value of a variable will not be changed again, also to ensure that it will not be operated on.

I will check the code to ensure that every var, even let, and all declarations use const.

2. There is no for loop

When learning a programming language, we will learn the for loop at the beginning. But since learning functional programming, I changed the for loop to use filter, map and reduce. For those that require some extra calculations, I would use recursion or a third-party library like lazy.js.

There is no for loop in my code now. If you see it, tell me and I will eliminate it.

3. if can also be simplified

I started to stop writing large chunks of code inside if. I extracted the logic inside and put it in a separate function. In this way, we can simplify if using the ternary operator (a?b:c).

There are almost no if statements in my code nowadays. To make it easier for other developers to understand my code, I rarely use it.

4. Say goodbye to switch

Similarly, I don’t like to use switch, but I am looking for a functional way of writing.

I also like to use Ramda's cond operator instead of swtich.

5. Don’t worry about this anymore

Yes, you heard it right! We can also eliminate this completely.

Functional JavaScript allows you to completely abandon the use of annoying this
Now there are only data and functions, and even data is just a special expression of the function, you no longer need this. I started to understand objects as states and functions in functional languages. I don't even need to bind state or functions to objects, like in OOP.

Object-oriented design is not necessary

Looking back now, I find that the complexity brought by object-oriented programming is really unnecessary. I can implement the same functionality and accomplish the same tasks using functional languages. Moreover, the code is lighter because there is no need to pass these complex objects around. There are only data and functions, and because functions are not bound to objects, they are easier to reuse. I no longer need to worry about all the problems that traditional prototypal inheritance brings. JavaScript is not designed well.

The lack of private, public, internal or protected access controllers for JavaScript is no longer an issue. Access controllers are designed to solve problems due to the introduction of object-oriented programming. In functional JavaScript, these problems no longer exist.

Summary

My code looks completely different now. It contains a lot of pure functions, which I made into different ES6 modules. These functions can be used to build more complex functions. A large number of functions are very simple one-line lambda expressions.

Now my thinking about software has changed: the input is a data stream, and then the program acts on the data stream to perform various operations on the data, and then returns new data.

The impact of functional design on programming languages ​​is ubiquitous. LINQ in C# is the best example. Similarly, Java 8 also introduces functional language features.

Related recommendations:

Detailed introduction to XML behavior (behavior)-new DHTML?

The above is the detailed content of Function sharing that enables JavaScript to break apart. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),