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 js writes code in a strongly typed style

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how js writes code in a strongly typed style, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article, so let's learn about it!

Write code in a strongly typed style

JS is weakly typed, but you can't write code too casually, which reflects a poor coding style. The following points are explained:

(1) when defining a variable, specify the type, tell the JS interpreter what data type the variable is, and don't let the interpreter guess, such as bad writing:

Var num, str, obj

Three variables are declared, but it doesn't really work, because the interpreter doesn't know what type they are, and a good way to write them would look like this:

Var num = 0, str ='', obj = null

When defining variables, give him a default value, which is not only convenient for the interpreter, but also for the person who reads the code, and he will know what these variables might be used for.

(2) do not change the type of variable at will, such as the following code:

Var num = 5; num = "-" + num

On line 1 it is an integer, and on line 2 it becomes a string. Because JS will eventually be interpreted as an assembly language, the type of assembly language variables must be determined, and if you change an integer into a string, the interpreter has to do some extra processing. And this coding style is not advocated, there is a variable line 1 is an integer, line 10 becomes a string, line 20 becomes an object, which makes people who read the code more confused, it is obviously an integer, how suddenly become a string. A good way to write it would be to define another variable in a string:

Var num = 5; var sign = "-" + num

(3) the return type of the function should be determined, for example, the following uncertain words:

Function getPrice (count) {if (count < 0) return ""; else return count * 100;}

The function getPrice may return an integer or an empty string. This is not a good way to write, although it conforms to the JS syntax, but this coding style is not good. People who use your function will be a little at a loss, afraid to add, subtract, multiply and divide directly, because the value is NaN if the string is returned for operation. The return type of the function should be determined, such as the following integer:

Function getPrice (count) {if (count < 0) return-1; else return count * 100;}

Then tell the user that if you return-1, it is illegal. If the type is determined, the interpreter does not have to do some extra work and can run faster.

The above is all the content of the article "how to write code in a strongly typed style in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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