This article describes several tips for writing JavaScript code in detail to help you improve your efficiency. I have been writing JavaScript code for a long time and cannot remember the beginning of my time. I am very excited about the achievements of the JavaScript language in recent years. I am lucky to be one of those achievements. I have written many articles, chapters, and a book devoted to it. However, I can still find some new knowledge about this language. The following describes the past, which makes it impossible for me to say "Ah !" You should try these skills now, instead of waiting for the future to discover them by chance.
Concise writing
One of my favorite in JavaScript is the shorthand method for generating objects and arrays. In the past, if you want to create an object, you need:
The Code is as follows:
Var car = new Object ();
Car. color = 'red ';
Car. wheels = 4;
Car. hubcaps = 'spinning ';
Car. age = 4;
The following statement can achieve the same effect:
The Code is as follows:
Var car = {
Color: 'red ',
Wheels: 4,
Hubcaps: 'spinning ',
Age: 4
}
It's much simpler. You don't need to use the name of this object repeatedly. In this way, the car is defined, and you may encounter invalidUserInSession, which only happens when you use IE. Remember that you don't need to write a comma in front of the right braces, so you won't be in trouble.
Another convenient shorthand is for arrays. The traditional method for defining arrays is as follows:
The Code is as follows:
Var moviesThatNeedBetterWriters = new Array (
'Transformers ', 'transformers2', 'Avatar', 'indianajones 4'
);
The short version is as follows:
The Code is as follows:
Var moviesThatNeedBetterWriters = [
'Transformers ', 'transformers2', 'Avatar', 'indianajones 4'
];
There is a problem with arrays. In fact, there is no graph group function. But you will often find someone defining the above car, just like this
The Code is as follows:
Var car = new Array ();
Car ['color'] = 'red ';
Car ['wheels'] = 4;
Car ['hubcaps'] = 'spinning ';
Car ['age'] = 4;
Arrays are not omnipotent; this write is incorrect and confusing. Graph groups are actually object functions, and people confuse these two concepts.
Another cool shorthand method is to use a Trielement conditional symbol. You don't have to write the following...
The Code is as follows:
Var direction;
If (x <200 ){
Direction = 1;
} Else {
Direction =-1;
}
You can use the ternary conditional symbol to simplify it:
The Code is as follows:
Var direction = x <200? 1:-1;
If the condition is true, the value following the question mark is used. Otherwise, the value following the colon is used.
Store data in JSON format
Before I found JSON, I used various crazy methods to store data in JavaScript's inherent data types, such as arrays, strings, there are symbols that are easy to split and other annoying things in the middle. After Douglas Crockford invented JSON, everything changed. Using JSON, you can use JavaScript's own functions to store data in complex formats, and you can directly access and use it without additional conversions. JSON is the abbreviation of "JavaScript Object Notation". It uses the two abbreviated methods mentioned above. So if you want to describe a band, you may write it like this:
The Code is as follows:
Var band = {
"Name": "The Red Hot Chili Peppers ",
"Members ":[
{
"Name": "Anthony Kiedis ",
"Role": "lead vocals"
},
{
"Name": "Michael 'flea' Balzary ",
"Role": "bass guitar, trumpet, backing vocals"
},
{
"Name": "Chad Smith ",
"Role": "drums, percussion"
},
{
"Name": "John Frusciante ",
"Role": "Lead Guitar"
}
],
"Year": "2009"
}
You can directly use JSON in JavaScript, encapsulate it in a function, or even use it as the return value form of an API. We call this JSON-P, and many APIs use this form.
You can call a data provider source to directly return JSON-P data in script code:
The Code is as follows:
Script
Function delicious (o ){
Var out ='
';
For (var I = 0; I Out + ='
- '+
O [I]. d +' ';
}
Out + ='
';
Document. getElementById ('delicmy'). innerHTML = out;
}
Script