Preface
Yesterday’s article mentioned an episode. The origin of the story is that a "little boss" in my WeChat suddenly came with an unspeakable expression ( Little (small) boss
"SB..."??? Haha, it's still abbreviated as "little"). Then started the following dialogue:
- Little: I'm so annoying about TM recently
- Me: what's the boss
- Xiao: The code I wrote has recently added new business changes to affect the whole body. The mentality is a little burst.
- Me: What's the specific situation? Let my little brother help you with your worries.
- Xiao: Didn't you just take charge of the project independently before? It doesn't feel too difficult to achieve from 0 to 1. But recently, some new business requirements have been added to the original basis and the previous code needs to be changed. I always encounter some problems when the global variables in a function are changed and many functions are invalid, which leads to the need to debug and track where they are used. . .
- Me: After getting the project/product requirements, I usually spend a lot of time thinking about the design ratio of 7:3 or even 8:2 before writing the code; experience is one aspect. Thinking is still very important. Returning to your question, say that you did not do some work in the early stage, such as:
coding design;
overall module division;
engineering framework (do some
unit tests, coverage tests...) and other measures, I will talk to you today How to solve these
small problems in detail!!
I started a series of actions of pretending and licking...
Back to the topic of today's article to introduce the programming paradigm, and how to use it JavaScript
What is a programming paradigm?
Programming paradigm ( Programming paradigm
) refers to a typical mode or method of programming in a computer.
The programming paradigm mainly includes: imperative programming ( Imperative
), declarative programming ( Declarative
) and functional programming ( Functional
)...
Imperative programming:
The main idea of imperative programming is to pay attention to the steps that the computer executes, that is, to tell the computer what to do first and then what to do step by step.
For example: if you want to output a variable and a combination of variables, you need to tell the computer like this:
- Step 1: Create a variable (name) name;
- Step 2: Create a variable (greeting) greeting;
- Step 3: console.log output
var name = "wlove"; //声明
var greeting = "hello, I'm ";//声明
console.log(greeting + name);//输出 hello, I'm wolove
Declarative programming:
Declarative programming is to express the logic of program execution in the form of data structure. Its main idea is to tell the computer what to do, but not to specify how to do it. The closest schedule is that the HTML and CSS used in web programming belong to declarative programming. (Explanation: HTML/CSS is actually structured and does not tell the computer what to do in detail. So it belongs to declarative programming
Declarative programming
; in fact, it is very similar to many configuration files.)
The most direct example is SQL
:
SELECT * FROM table WHERE num < 5
By observing the code of declarative programming, we can find that it has a feature that it does not need to create variables to store data.
Another feature is that it does not contain loop control codes such as for and while.
Functional programming:
Functional programming and declarative programming are related because they have the same idea: they only focus on what to do instead of how to do it. But functional programming is not limited to declarative programming.
The most important feature of functional programming is the "function first", that is, functions can appear anywhere, for example, you can pass a function as a parameter to another function, not only that, you can also use a function as a return value. It is very important; if you use this programming properly, you only need to consider one stream of input and output. What object (prototype) and its side effects (interaction effects) do not need to be considered.
Speaking of functional programming, friends who may come into contact with other languages will recall Haskell
, Clojure
these languages. In fact, most common programming languages have already provided support for this programming method; for example, today's protagonist JavaScript
.
Compared to the previous imperative programming; declarative programming, I prefer to use functional programming. First, I experimented with object-oriented (prototype) programming. Very confused or painful; everydebug
quite refreshing; secondly, functional programming can make the logic of the code clearer and more elegant; safer. (short, personal use is very cool)
Several introductions to functional programming in JavaScript:
Speaking of functional programming, the most basic need to consider side effect
(no side effects) and pure
(pure); if implemented in this way, a very important problem of the little boss can be solved reasonably debug
A variable has been tracking where it has been modified...;
//例子1 纯函数
function greet(name){
return "hello I'm " + name;
}
greet("wlove") // hello I'm wlove
//例子2 非纯函数 用到了外部的变量
var name = "wlove"
function _fn(name){
return pre + str
}
greet(name)// hello I'm wlove
Functional programming also has a characteristic higher-order function (also an important knowledge point for promotion); a function can be returned as a function.
//高阶函数:
function greet(greeting){
return function(name){
return greeting + " " + name
};
}
var GREETING = greet("hello I' m");
GREETING("wlove"); // "hello I' m wlove"
Functional programming also has a problem to think about; if you say that a Copy object is regenerated every time (for example, if the input parameter is array
); it is regenerated every time it changes. That will increase the use of space. How to solve this problem? You can think about it or check some information. Leave a comment to discuss.
At last
Life diary from the previous article: 20210524 The repetition of daily life. Meeting; learning; writing code; running; writing articles; recording videos; visiting the community... and it’s over if you are happy.
This article is mainly because I especially want to help (tian) the little boss haha; it can be regarded as writing as you want; everyone is welcome to leave a message and discuss. The following comments will supplement the introduction of how to effectively solve the space problem.
chat, my little boss just exaggerated, but he didn't know that the practice was not practiced. I have to take the initiative to chat with others hehe
Next JS engine, as a front end, what needs to be recognized and how to learn.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。