Lecture
One of the most important skills of a programmer is the ability to name variables correctly .
There are only two restrictions on the name of a variable in JavaScript.
$
and _
For example:
var myName; |
var test123; |
What is especially interesting here is that the dollar '$'
and the underscore character '_'
are the same ordinary characters as the letters:
1 |
var $ = 5; // объявили переменную с именем '$' |
2 |
var _ = 15; // переменная с именем '_' |
3 |
4 |
alert($); |
And such variables would be wrong:
var 1a; // начало не может быть цифрой |
var my-name; // дефис '-' не является разрешенным символом |
The variables apple
and AppLE
are two different variables.
You can use Russian letters:
1 |
var имя = "Вася" ; |
2 |
alert(имя); |
There is a list of reserved words that cannot be used when naming variables, as they are used by the language itself, for example: var, class, return, implements
, etc.
Some words, such as class
, are not used in modern JavaScript, but they are busy for the future. Some browsers allow you to use them, but this can lead to errors.
The following example will work in many older browsers that allow the use of the word 'class'
and will not work in modern ones. They will give a syntax error, try, run:
1 |
var class = 5; |
2 |
alert(class); |
Choosing the right variable name is one of the most important and difficult things in programming that distinguishes the beginner from the guru.
The fact is that most of the time we spend not on the original writing of the code, but on its development.
What is development? This is when I wrote code yesterday, and today (or a week later) I come and I want to change it. For example, to display a message wrongly, but in this way .. Process the goods differently, add functionality .. And where do I keep the message there? ..
It is much easier to find the necessary data if they are correctly labeled, i.e. The variable is named correctly .
Unacceptable:
var moiTovari; |
var cena; |
var ssilka; |
Will fit:
var myGoods; |
var price; |
var link; |
If you suddenly do not know English - it's time to learn. I still have to ...
Name variables with no semantic meaning, such as a
, e
, p
, mg
, can only be used if they are used in a small code fragment and their use is obvious.
In general, the name of the variable should be clear. Sometimes you need to use a few words.
вместеВотТак
.
For example:
var borderLeftWidth; |
This way of recording is called “camel notation” or, in English, “camelCase”.
There is an alternative standard when several words are written with an underscore '_'
:
var border_left_width; |
Mostly JavaScript uses the borderLeftWidth
variant, in particular in the built-in language and browser functions. Therefore, it is advisable to stay on it.
Another reason to choose “camel notation” is to write a bit shorter in it than with underscore, since no need to insert '_'
.
Coming up with such names - both short and precise, comes with experience, but only if you consciously strive for it.
Let me share one little secret that will improve your variable names and save you time.
It happens that you wrote the code, after a while you come back to it, and you need to fix something. For example, change some border “ border
”
... And you remember that the variable in which the value you want is stored is called something like this: borderLeftWidth
. You are looking for it in the code, do not find, understand, and discover that in fact the variable was called like this: leftBorderWidth
. Then make the necessary changes.
In this case, the best move is to rename the variable to the one you were looking for initially . That is, you have leftBorderWidth
in the code, and you rename it to borderLeftWidth
.
What for? The fact is that the next time you want to correct something, you will search by the same name. Accordingly, it will save you time.
In addition, since it is this variable name that occurred to you - most likely, it corresponds more to the data stored there than the one you originally invented.
The meaning of the variable name is “the name on the box”, according to which we can find the data we need as quickly as possible.
Do not be afraid to rename variables if you come up with a better name. Modern editors allow you to do this very conveniently. This will ultimately save you time.
There are lazy programmers who, instead of declaring a new variable, use an existing one.
As a result, it turns out that such a variable is like a box into which one, then another, then the third is thrown, without changing the name. What is in it now? And who knows .. You need to come, check.
Such a programmer will save time on declaring a variable - will lose two times more on debugging code.
The "extra" variable is good, not evil.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone