Home > Web Front-end > JS Tutorial > body text

Summary of seven details that JavaScript beginners should pay attention to_javascript skills

WBOY
Release: 2016-05-16 17:56:40
Original
996 people have browsed it

Each language has its own special features. For JavaScript, you can use var to declare variables of any type. This scripting language seems very simple, but writing elegant code requires continuous accumulation of experience. This article lists seven details that JavaScript beginners should pay attention to and share them with you.
(1) Simplify the code
It is very simple to define objects and arrays in JavaScript. We want to create an object, which is usually written like this:

Copy code The code is as follows:

var car = new Object();
car.colour = 'red';
car. wheels = 4;
car.hubcaps = 'spinning';
car.age = 4;

The following writing can achieve the same effect:
Copy code The code is as follows:

var car = {
colour:'red',
wheels:4,
 hubcaps:'spinning',
 age:4
}

The following is much shorter, and you don't need to write the object name repeatedly.
In addition, there is also a concise way to write arrays. In the past, we declared arrays as follows:
Copy code The code is as follows:

var moviesThatNeedBetterWriters = new Array(
'Transformers','Transformers2','Avatar','Indiana Jones 4'
);

More The concise way to write it is:
Copy the code The code is as follows:

var moviesThatNeedBetterWriters = [
'Transformers','Transformers2','Avatar','Indiana Jones 4'
];

For arrays, there is a special thing like associative arrays. You will find that a lot of code defines objects like this:
Copy the code The code is as follows:

var car = new Array();
car['colour'] = 'red';
car['wheels'] = 4;
car['hubcaps'] = 'spinning';
car['age'] = 4;

This is crazy, don't get confused, "associative array" is just an alias for an object.
Another way to simplify the code is to use the ternary operator, for example:
Copy the code The code is as follows:

var direction;
if(x < 200){
direction = 1;
} else {
direction = -1;
}

We can use the following code to replace this writing:
Copy the code The code is as follows:
var direction = x < 200 ? 1 : -1;

(2) Use JSON as data format
The great Douglas Crockford invented the JSON data format to store data , you can use native javascript methods to store complex data without any additional conversion, for example:
Copy code 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 use JSON directly in JavaScript, or even as a format returned by API, which is used in many APIs, such as:
Copy code The code is as follows:

<script> <br>function delicious(o){ <br>var out = '<ul>'; <br> for(var i=0;i<o.length;i ){ <BR>out = '<li><a href="' o[i].u '">' <br>o[i ].d '</a></li>'; <br>} <br>out = '</ul>'; <br>document.getElementById('delicious').innerHTML = out; <br>} <br></script>
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!