Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to summarize the seven main JavaScript concepts

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to summarize the seven major JavaScript concepts. The editor thinks it is very practical, so I hope you can get something after reading this article. Let's take a look at it with the editor.

The purpose of the term is to encapsulate a particular idea into a beautiful and compact word. However, if you do not understand the meaning of the word, it will lose its meaning.

In the developer world, more advanced topics are often out of reach and often deter new developers. Partly because they seem to be reading a foreign novel. Letters and words may look familiar, but they have no meaning.

And it's hard to understand everything, especially if you have to keep stopping the second word to figure out what the sentence is trying to tell you.

Each term is a topic in itself, but for the purpose of space and readability, I simplify it as much as possible to the core idea.

1. Invariance

When the shape of the data remains the same.

Therefore, if you have an object that enters a function, it comes out of it in exactly the same form. The data attached to it can be changed, but the number, name and order of parameters cannot be changed.

For example, this is a mutation:

Function changeMe (someObject) {someObject.age = 3; return someObject;} let exampleOne = {"cat": "Tibbers"}; console.log (changeMe (exampleOne))

This function changes the shape of the object, which means it is mutable.

two。 Declarative form

No matter in what order you operate, the basic rules ensure that you get the same and correct results every time.

For example, the mathematical equation follows a declarative method.

(2 x 5) + 8-3 = 15

Therefore, if you move the order, you will still get the same result.

-3 + (2 x 5) + 8 = 158-3 + (2 x 5) = 15

In JavaScript programming, declarative mode is where the order of functions is irrelevant to the construction of the final result. You can call them in any order. The order doesn't matter.

3. Recursion

When the function continues to call itself until certain conditions are met.

No, it's not a for loop. It may sound like this, but it is not.

The for loop is the conditional method of JavaScript. Recursion is a complete function that constantly calls itself.

This means that there are two parts of recursion-calls based on specific conditions and the exit clause. Your exit clause is basically what happens at the end of the recursion.

This is a simple potential recursion:

Function sumProfit (sales) {if (/ / condition for ending) {return theFinalObjectOrResult;} else {/ / do something. Reduce the condition. Return sumProfit (reducedCondition);}}

4. Callback

A callback is a function that is executed after another function has been executed.

Why do we need this? Because JavaScript is event-driven-it means it doesn't wait for a response. It doesn't matter when things run in a self-sufficient way.

The situation becomes serious when you start to rely on external responses, such as API. Latency is the time it takes your JavaScript code to send a request and receive it back.

From JavaScript's point of view, it has done its job-it has successfully executed the code.

However, in fact, you are still waiting for a response. Sometimes, you use promises or delay timers to force the code to calm down, relax, and wait for a while. After confirming that it is complete, you can call the callback () function.

Anyway, this is what the callback looks like:

Function waitForMeeeee (someValues, callback) {/ / do something with someValues callback ();} waitForMeeeee ('The answer is 42, function () {alert (' all done now');})

You can also abstract the callback and make it look like this:

Function waitForMeeeee (someValues, callback) {/ / do something with someValues callback ();} function allDone () {alert ('all done now');} waitForMeeeee (' The answer is 42, allDone)

5. Async

Think of a straight line. Your JavaScript code executes from one end to the other. But sometimes you need to pause, only when you run to an external source to grab something.

The moment of pause is the asynchronous part of the JavaScript.

The keyword async also returns an implicit Promise.

What do you mean, implicit? By default, it returns a promise.

What is a promise? This is what tells your code to wait, because there is a delay in completing some work. For example, you are waiting for an external API to respond with the correct data.

Why is this important? Because you can use then () with asynchronous functions.

So what is ()? Your callback is equivalent to a commitment-based function that allows you to perform actions after the commitment is completed.

What does it look like? This is an example:

Async function doSomething () {/ / do something there. Return'wootballs;} doSomething () .then (function (result) {console.log (result);})

Or, if you really want to make the commitment part clear:

Async function doSomething () {/ / do something there. Return Promise.resolve ('wootworthy');} doSomething () .then (function (result) {console.log (result);})

6. Agent

Think of it as an additional extension of the object. You can create custom behaviors for things that already exist.

To some extent, it also acts as an intermediary between the original object and other functions.

Yes. Agents can change and process data.

Yes. It is commonly used, among other things, as a validation checker.

How does it work?

The agent consists of three parts-the handler, the trap (that is, the method), and the target.

There are 13 traps available for agents. You need to Google them because this is beyond the scope of this article.

This is an example of an agent that has implemented a trap:

Let handler = {get: function (theObjectPassed, theObjectName) {/ / some checking logic console.log (theObjectPassed, theObjectName); return 'all done';}} let someObject = {a: 1, b: 2}; let valueName = new Proxy (someObject, handler); console.log (valueName.someObject)

The function of the agent is much more than that, but the example is only a starting point.

7. Garbage collection

Everything takes up memory. After the variable is initialized, a little space is allocated to it.

Memory is initialized only when it is called. Therefore, when a function runs and there are variables inside, it only lasts for as long as the function needs it. It doesn't stay around.

Garbage collection is a way to clear memory.

A memory leak is a situation in which garbage collection is not performed because variables are declared in global space, which can cause pollution and take up unnecessary space.

This is too much, and your application may slow down.

Therefore, keep the variables as much as possible and use null to uninitialize unwanted content

The above is how to summarize the seven major JavaScript concepts, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report