This article describes seven tips that are not familiar with but useful in JavaScript. The success of JavaScript is repeat. Writing JavaScript code for Web pages is already the basic skill of all Web designers. This interesting language contains many unfamiliar things, even if JavaScript programmers who have been writing JavaScript for many years, not completely thorough. This article describes seven tips that are not familiar with but useful in JavaScript.
Simple statement
JavaScript can use simple statements to quickly create objects and arrays, such as the following code:
The Code is as follows:
Var car = new Object ();
Car. color = 'red ';
Car. wheels = 4;
Car. hubcaps = 'spinning ';
Car. age = 4;
You can use the following simple statement:
The Code is as follows:
Var car = {
Color: 'red ',
Wheels: 4,
Hubcaps: 'spinning ',
Age: 4
}
The object car is created here, but note that do not add ";" before ending the curly brackets; "Otherwise, it will be very troublesome for IE.
The traditional method for creating arrays is:
The Code is as follows:
Var moviesThatNeedBetterWriters = new Array (
'Transformers ', 'transformers2', 'Avatar', 'Indiana Jones 4'
);
Use the simple statement:
The Code is as follows:
Var moviesThatNeedBetterWriters = [
'Transformers ', 'transformers2', 'Avatar', 'Indiana Jones 4'
];
Another question about arrays is whether there is an associated array. You will find a lot of code examples to define examples like this on the car
The Code is as follows:
Var car = new Array ();
Car ['color'] = 'red ';
Car ['wheels'] = 4;
Car ['hubcaps'] = 'spinning ';
Car ['age'] = 4;
Another simple statement that can be used is the condition judgment statement:
The Code is as follows:
Var direction;
If (x <200 ){
Direction = 1;
} Else {
Direction =-1;
}
It can be simply as follows:
The Code is as follows:
Var direction = x <200? 1:-1;
JSON Data Format
JSON, short for "JavaScript Object Notation", is designed by Douglas Crockford. JSON changes the difficulty of JavaScript in caching complex data formats. For example, if you want to describe a band, you can write as follows:
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, or even return data objects as some APIs. The following code calls an API of delicious.com, the famous bookmarkdon.com, and returns all your bookmarks on the website, and displayed on your own website:
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