Some insightful JavaScript skills _ javascript skills

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.