Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the programming specifications in JavaScript

Shulou Source: shulou.com Published: 2022-06-02 08:24:21 09月15日 Update

What are the programming specifications in JavaScript, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, hope you can gain something.

Background

JavaScript is a client-side scripting language that is used by Web projects. This guide lists the rules to follow when writing JavaScript.

JavaScript language specification variable

Var must be added to declare a variable

Keywords:

Var A1 = 1 * var b1 = 11

When you don't write var

Variables are exposed to the global context, which is likely to conflict with existing variables In addition, if not added, it is difficult to determine the scope of the variable, the variable is likely to be in the local scope, it is easy to leak into Document or Window, so be sure to use var

Variable.

Constant

Constant forms such as: NAMES_LIKE_THIS, even if you use uppercase characters and underscores, you can use the @ const tag to indicate that it is a constant, but do not use the const keyword.

For basic types of constants, you only need to convert the naming:

/ * The number of seconds of minute. * @ type {number} * / eflag.example.SECONDES_IN_A_MINUTE = 60

For non-basic types, use @ const

Tag:

/ * The number of seconds in each of the given units. * @ type {Object.} * @ const * / eflag.example.SECONDS_TABLE = {minute: 60 department hour: 60 * 60 department day: 60 * 60 * 24}

As for the keyword const, do not use it because IE is not recognized.

semicolon

Always use a semicolon. If you only rely on implicit segmentation between statements, it can be troublesome sometimes. Using a semicolon, you will know better where the statement begins and ends.

Semicolon at the end of line:

Var foo = 1) bar = 2) Baz = 3) obj = {foo: 1) bar: 2); single quotation marks ('') and double quotes ("")

Because JavaScript can recognize both single and double quotes as strings, in order to unify the specification, the definition of strings in JavaScript requires the use of single quotes:

Var val ='a'

Again, attributes in html use double quotes:

When dynamically generating html tags in JavaScript:

Var _ input =''; space

Five spaces between parameters and parentheses:

Function fn (arg1, arg2) {}

There is a space after the colon

{foo: 1,bar: 2,baz: 3}

Conditional statements have spaces

If (true) {} while (true) {} switch (v) {} Tips and TricksTrue and False Boolean expressions

The following Boolean expressions all return false:

Nullundefined'' empty string 0

The number 0 can be careful of the following, but all return true:

'0' string 0 [] empty array {} empty object

If you want to check whether the string is null or empty:

If (y! = null & & y! =') {}

It would be better to write like this:

If (y) {} conditional (ternary) operator (?:)

The ternary operator replaces the following code:

If (val! = 0) {return foo ();} else {return bar ();}

You can write as follows:

Return val? Foo (): bar ()

It is also useful when generating HTML code:

Var html =''; & & and | |

The binary Boolean operator can be short-circuited and the last term is calculated only when necessary. | | it is called the default operator, because it is possible to:

/ * * @ param {* =} opt_win * / function foo (opt_win) {var win; if (opt_win) {win = opt_win;} else {win = window;} / /.}

You can use it to simplify the above code:

/ * * @ param {* =} opt_win * / function foo (opt_win) {var win = opt_win | | window; /...} use join () to create a string

It is usually used like this:

Function listHtml (items) {var html ='

Tags: Character variable string quotation mark face semicolon constant operator code keyword Boolean array statement clarity action condition tag space type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Xiaomi vpn macOS