Seven details for beginners of JavaScript _ basic knowledge

Source: Internet
Author: User
All languages have their own special features. For JavaScript, using var can declare any type of variables. This script language looks very simple, however, if you want to write elegant code, you need to constantly accumulate experience. Next, we will introduce that Beginners should pay attention to the special features of various languages. For JavaScript, using var can declare any type of variables. This script language looks simple, but to write elegant code, it requires constant experience. This article lists seven details that JavaScript Beginners should pay attention.
(1) simplified code
--------------------------------------------------------------------------------
It is very simple to define objects and arrays in JavaScript. We want to create an object, which is generally written as follows:

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
}


The subsequent statement is much shorter, and you do not need to repeat the object name.
In addition, there is a simple way to write arrays. In the past, we declared arrays as follows:

The Code is as follows:


Var moviesThatNeedBetterWriters = new Array (
'Transformers ', 'transformers2', 'Avatar', 'Indiana Jones 4'
);


A more concise statement is as follows:

The Code is as follows:


Var moviesThatNeedBetterWriters = [
'Transformers ', 'transformers2', 'Avatar', 'Indiana Jones 4'
];


For arrays, there is also a special thing like associating arrays. You will find that many codes define objects as follows:

The Code is as follows:


Var car = new Array ();
Car ['color'] = 'red ';
Car ['wheels'] = 4;
Car ['hubcaps'] = 'spinning ';
Car ['age'] = 4;


This is crazy. Don't be confused. "Join array" is just an alias of an object.
Another way to simplify the code is to use the ternary operator. For example:

The Code is as follows:


Var direction;
If (x <200 ){
Direction = 1;
} Else {
Direction =-1;
}


We can replace this statement with the following code:

The Code is as follows:


Var direction = x <200? 1:-1;


(2) Use JSON as the data format
The great Douglas Crockford invented the JSON data format to store data. You can use the native javascript method to store complex data without any extra conversion. For example:

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 the API, which is applied in many APIs, for example:

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.