Home > Web Front-end > JS Tutorial > Some JavaScript skills that I haven't seen before_javascript skills

Some JavaScript skills that I haven't seen before_javascript skills

WBOY
Release: 2016-05-16 18:28:40
Original
1163 people have browsed it

The success of JavaScript is widely talked about. Writing JavaScript code for Web pages has become a basic skill for all Web designers. This interesting language contains many unknown things that even JavaScript programmers with many years of experience have not fully understood. . This article talks about those techniques in JavaScript that you are not very familiar with but are very practical from 7 aspects.
Abbreviated Statements
JavaScript can use abbreviated statements to quickly create objects and arrays, such as the following code:

Copy code The code is as follows:

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

You can use abbreviated statements as follows:
Copy code The code is as follows:

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

The object car is created, but special attention needs to be paid. Be sure not to add ";" before the closing curly brace, otherwise you will encounter big trouble in IE.
The traditional way to create an array is:
Copy code The code is as follows:

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

Use abbreviated statements:

Copy code The code is as follows:

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

Another question about arrays is whether there is such a thing as an associative array. You will find a lot of code examples defining the above car like this example
Copy the code The code is as follows:

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


Another place where abbreviated statements can be used is conditional statements:
Copy code The code is as follows:

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

can be shortened to:
Copy codeThe code is as follows:
var direction = x < 200 ? 1 : -1;


JSON data format
JSON is the abbreviation of "JavaScript Object Notation" , designed by Douglas Crockford, JSON changes JavaScript's dilemma in caching complex data formats, as in the following example, if you want to describe a band, you can write like this:
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, even as the return data object of some APIs. The following code calls an API of the famous bookmark website delicious.com and returns the All bookmarks of this website and display them on your own website:
Copy the 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