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

The use of javascript values and semicolons

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

Share

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

This article focuses on "the use of javascript values and semicolons". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the use of javascript values and semicolons.

Value

Sometimes I wonder how the javascript parsing engine distinguishes the value of a variable, such as the following code.

Var x = 'javascript'; / / javascriptx = "hello"; / / hellox = 555; / / 555x = null; / / nullx = a; / / an is not definedx = true; / / true

A number is assigned directly because it has no diversity, and a number is a number. However, it is difficult to distinguish between cases where the value is English, because in a programming language, English can be either a string or another variable referenced. Therefore, how to distinguish between variables and strings is particularly important. Programming languages often enclose strings in quotation marks to distinguish between variables and strings. Some languages, such as java, also distinguish between single quotation marks and double quotation marks, which enclose a character while double quotation marks enclose a string. But javascript does not distinguish between characters and strings, but treats them both as strings, so there is no difference between single and double quotes in javascript.

Although quotation marks can be used to distinguish between variables and strings, a value can often be a keyword. For example, I assigned x to null in the above code, so how do these programming languages distinguish between variables and keywords?

Null = 123 Uncaught ReferenceError console.log (null); / / Uncaught ReferenceError: Invalid left-hand side in assignmentundefined = 456 shareholders console.log (undefined); / / undefined

Above I assigned another value to null and undefined respectively. As a result, an error was reported in the assignment to null. Although no error was reported in the assignment to undefined, it was not successful. Maybe for null and undefined, they are values. The variable is to find the value. When we talk about how javascript distinguishes variables from keywords, it may eventually become how javascript distinguishes variables from values.

semicolon

In some JS plug-ins, you will often see a line of code like this

(function () {.}) ()

There is a semicolon at the front of the code, so what is the semicolon for?

We know that a semicolon represents the end of a piece of code, but the problem is that javascript allows you not to write a semicolon, so there is a problem, the end of the code is not decided by you but by the program, and the program is not omnipotent, often it just follows a certain rule, and if the code you write does not conform to its rules, the end result will be somewhat unsatisfactory.

Here are javascript's parsing rules for omitting semicolons

Var axi1 + 2console.log (a) / / 3

The javascript parser parses the above code into

Var a = 1 + 2 × console.log (a); / / 3

If javascript does not add a semicolon after 2, it will not be parsed, so to speak, if it cannot be parsed, the javascript parser will try to add a semicolon to it, and an error will be reported if it still cannot be parsed. Or for example, the following code

Var a = 10 b is not a function var b = 5 Poland var c = a + b (a + b).

It says that b is not a function, which means that the above code is probably parsed into the following code

Var a = 10 countries var b = 5 countries var c = a + b (a + b) .toString ()

It treats () as a function call. It can also be understood that the javascript parser will match as many as possible, but there are a few exceptions. They are retrun, break and continue. When the javascript parser parses these keywords, it will not regard the content after the line break as its own, but directly add the semicolon before the line break. Take a look at the following code

Function test () {return 123;} console.log (test ()); / / undefined

It doesn't return 123, which means it adds a semicolon directly after retrun.

Looking back, why did plug-in developers add a semicolon to the first line of code?

Since it is a plug-in, it is for others to use, right, but the key problem is that you do not know how the code is written by the people who use the plug-in. This seems to be quite absurd. What does its code have to do with us?

If the user's code affects our code, how does it affect it? For example, we are writing a piece of code like this

The first script is written by the user, and the second script is introduced by a plug-in, so how does the browser parse these two scripts? Why don't we test it?

Test.js

Var aa

Zmz.js

(1 / 2)

If you run it, you will find that there is no error, which means that the javascript parser will not parse with the code in the latter file because the previous file does not have a semicolon.

That's not the problem, but maybe you just read a book about HTTP. Wow, merging files can reduce the number of requests, so the two scripts are integrated. Changed into something like this

Var aa (1x 2)

Do you think this can't go wrong? if we had added a semicolon at the beginning of the plug-in, it wouldn't have happened.

Var aa; (1x 2)

So don't think of the semicolon as just to end a piece of code, it can also be used to isolate a piece of code from others.

At this point, I believe you have a deeper understanding of the use of javascript values and semicolons, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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