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

What are the knowledge points of JavaScript coding specification

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

Share

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

This article mainly introduces "what are the knowledge points of the JavaScript coding specification". In the daily operation, I believe that many people have doubts about the knowledge points of the JavaScript coding specification. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the questions of "what are the knowledge points of the JavaScript coding specification?" Next, please follow the editor to study!

1. Variable naming convention

Variable names include global variables, local variables, class variables, function parameters, and so on, all of which fall into this category.

Basic norm

Variable names are made up of type prefixes + meaningful words, and the first letters of words need to be capitalized. For example: sUserName,nCount.

Prefix specification

Each local variable needs to have a type prefix, which can be divided into:

S: represents a string. For example: sName,sHtml

N: represents a number. For example: nPage,nTotal

B: represents logic. For example: bChecked,bHasLogin

A: represents an array. For example: aList,aGroup

R: represents a regular expression. For example: rDomain,rEmail

F: represents a function. For example: fGetHtml,fInit

O: represents other objects not covered above, for example: oButton,oDate

Exception:

1: temporary variables with limited scope can be abbreviated, such as: str,num,bol,obj,fun,arr.

2: loop variables can be abbreviated, for example: iMagol jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjk.

Why is it necessary to force the definition of variable prefixes in this way? Officially because javascript is caused by a weak language. When defining a large number of variables, we need to know exactly what attributes the current variable is, and it is difficult to distinguish it only by ordinary words.

For example:

Var group = []; group.name = 'myGroup'; / * some code * * / / can you still tell what group is at a glance?

Another example is:

Var checked = false; var check = function () {return true;} / * * some code * * / if (check) {/ / it is possible to write checked as check, resulting in a logic error / / do some thing} due to the inability to quickly discover that check is a function

If we write as follows:

Var bChecked = false; var fCheck = function () {return true;} / * * some code * * / if (bChecked) {/ / do some thing} if (fCheck ()) {/ / do other thing}

It's a lot clearer.

Global variables and constant specifications

NetEase mailbox front-end is based on the concept of "class" to develop javascript (will be specifically introduced later), each class definition is in a closure function, in addition to the definition of a class under window, only two kinds of variables are allowed to be defined globally, that is, global variables and constants.

Global variables are prefixed with g and are defined under window. For example, gUserName,gLoginTime.

Some variables that are not allowed to modify values are considered constant and all letters are capitalized. For example: COPYRIGHT,PI. Constants can exist either in a function or globally.

It is easy to see why it is defined this way by looking at an example:

Var userName = "dongua"; function checkName (userName) {/ / there is a function parameter userName and a global variable userName. If you want to compare whether the two values are equal, you must write return window.userName = = userName}.

If you use the prefix of the global variable, it is very clear.

two。 Function naming specification

Uniformly use verbs or verb [+ noun] forms, such as fGetVersion (), fSubmitForm (), fInit (); functions that return logical values can use logical words such as is,has instead of verbs.

If there is an internal function, use the form of _ f + verb [+ noun]. The internal function must be defined in the function *. For example:

Function fGetNumber (nTotal) {if (nTotal)

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