each language has its own special place, for JavaScript, the use of Var can declare any type of variable, the scripting language seems very simple, but to write elegant code is the need for continuous accumulation of experience. This article provides a list of seven details that JavaScript beginners should pay attention to, and share them with you.
(1) simplifies code
JavaScript defines objects and arrays very simply, and we want to create an object that is generally written like this:
1 |
var car = new Object (); |
4 |
Car.hubcaps = ' spinning '; |
the following wording can achieve the same effect:
is much shorter, and you don't have to repeat the object name.
also has a concise way of writing the array, which we have declared in the past:
1 |
var moviesthatneedbetterwriters = new Array ( |
2 |
' Transformers ', ' Transformers2 ', ' Avatar ', ' Indiana Jones 4 ' |
more concise writing is:
1 |
var moviesthatneedbetterwriters = [ |
2 |
' Transformers ', ' Transformers2 ', ' Avatar ', ' Indiana Jones 4 ' |
for arrays, there is an associative array of such a special thing. You'll find that a lot of code defines objects like this:
1 |
var car = new Array (); |
2 |
car[' colour '] = ' red '; |
4 |
car[' hubcaps '] = ' spinning '; |
This is crazy, don't be confused, associative array is just an alias for an object.
another way to simplify the code is to use the ternary operator, for example:
we can use the following code to replace this type of notation:
1 |
var direction = x < 200? 1:-1; |
(2) uses JSON as the data format
great Douglas Crockford invented the JSON data format to store data, you can use native JavaScript methods to store complex data without any additional transformations, such as:
02 |
"Name": "The Red Hot Chili Peppers", |
05 |
"Name": "Anthony Kiedis", |
06 |
' Role ': ' Lead vocals ' |
09 |
"Name": "Michael ' Flea ' Balzary", |
10 |
"Role": "Bass guitar, trumpet, backing vocals" |
14 |
"Role": "Drums,percussion" |
17 |
"Name": "John Frusciante", |
18 |
' Role ': ' Lead Guitar ' |
you can use JSON directly in JavaScript, or even a format returned as an API, to be applied in many APIs, such as:
01 |
<div id= "Delicious" ></div><script> |
02 |
function Delicious (o) { |
04 |
for (Var i=0;i<o.length;i++) { |
05 |
out = = ' <li><a href= ' + o[i].u + ' > ' + |
06 |
O[I].D + ' </a></li> '; |
09 |
document.getElementById (' delicious '). InnerHTML = out; |
12 |
<script src= "Http://feeds.delicious.com/v2/json/codepo8/javascript?count=15&callback=delicious" ></ Script> |
here, call the delicious Web service to get the latest bookmarks, return in JSON format, and then display them in the form of unordered lists.
Essentially, JSON is the most lightweight way to describe complex data, and it runs directly in the browser. You can even call the Json_decode () function in PHP to use it.
(3) Try to use JavaScript native function
to find the largest number in a set of numbers, we might write a loop, for example:
1 |
var numbers = [3,342,23,22,124]; |
3 |
for (Var i=0;i<numbers.length;i++) { |
4 |
if (Numbers[i] > max) { |
in fact, do not have to loop to achieve the same function:
1 |
var numbers = [3,342,23,22,124]; |
2 |
Numbers.sort (function (a,b) {returnb-a}); |
and the most concise writing is:
1 |
Math.max (12,123,3,2,433,4); Returns 433 |
you can even use Math.max to detect which attributes the browser supports:
1 |
var scrolltop= math.max ( |
2 |
Doc.documentElement.scrollTop, |
If you want to add a class style to an element, it might be the original way of writing:
1 |
function AddClass (elm,newclass) { |
3 |
Elm.classname = (c = = "")? Newclass:c+ ' +newclass; |
The
and more elegant writing is:
1 |
function AddClass (elm,newclass) { |
2 |
var classes = Elm.className.split ('); |
3 |
Classes.push (Newclass); |
4 |
Elm.classname = Classes.join ("); |
(4) event delegate
The
event is a very important part of JavaScript. We want to bind a link in a list to a point hit event, the general practice is to write a loop, to each linked object binding events, the HTML code is as follows:
3 |
<li><a href= "HTTP://OPERA.COM/WSC" >opera WEB standards curriculum</a></li> |
4 |
<li><a href= "http://sitepoint.com" >Sitepoint</a></li> |
5 |
<li><a href= "http://alistapart.com" >a List apart</a></li> |
6 |
<li><a href= "http://yuiblog.com" >yui blog</a></li> |
7 |
<li><a href= "http://blameitonthevoices.com" >blame it on the voices</a></li> |
8 |
<li><a href= "http://oddlyspecific.com" >oddly specific</a></li> |
script is as follows:
01 |
Classic Event Handling Example |
03 |
var resources = document.getElementById (' resources '); |
04 |
var links = resources.getelementsbytagname (' a '); |
05 |
var all = Links.length; |
06 |
for (Var i=0;i<all;i++) { |
07 |
Attach a listener to each link |
08 |
Links[i].addeventlistener (' click ', Handler,false); |
10 |
function Handler (e) { |
11 |
var x = E.target; Get the link this is clicked |
is more reasonable to write only to the parent object of the list to bind the event, the code is as follows:
02 |
var resources = document.getElementById (' resources '); |
03 |
Resources.addeventlistener (' click ', Handler,false); |
04 |
function Handler (e) { |
05 |
var x = E.target; Get the link tha |
06 |
if (x.nodename.tolowercase () = = = ' a ') { |
07 |
Alert (' Event delegation: ' + x); |
(5) anonymous function
One of the biggest headaches of
about JavaScript is that its variables don't have a specific scope. In general, any variable, function, array, or object is global, which means that other scripts on the same page can access and overwrite them. The workaround is to encapsulate the variable in an anonymous function. For example, the following definition produces three global variables and two global functions:
3 |
var status = ' single '; |
4 |
function Createmember () { |
7 |
function Getmemberdetails () { |
after encapsulation as follows:
01 |
var myapplication = function () { |
04 |
var status = ' single '; |
06 |
Createmember:function () { |
09 |
Getmemberdetails:function () { |
14 |
Myapplication.createmember () and |
15 |
Myapplication.getmemberdetails () now works. |
This is called the monomer mode, is a JavaScript design pattern, this pattern in Yui used very much, the improvement is written:
01 |
var myapplication = function () { |
04 |
var status = ' single '; |
05 |
function Createmember () { |
08 |
function Getmemberdetails () { |
16 |
Myapplication.get () and myapplication.create () now work. |
(6) code can be configured
your code if you want to make it easier for others to use or modify, you need to be configurable, and the solution is to add a configuration object to the script that you are writing. The main points are as follows:
1, add an object called configuration in your script.
2, in the configuration object to store all other people may want to change things, such as the CSS ID, class name, language and so on.
3, returns this object as a public property so that others can override it.
(7) Code compatibility
compatibility is a beginner's easy to ignore part, usually when learning JavaScript is in a fixed browser to test, and this browser is probably IE, this is very fatal, because the current several major browsers, ie to the standard of the support is the worst. What the end user sees may be that the code you write is not working correctly in a browser. You should test your code in a mainstream browser, which may be time-consuming, but it should be done.
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121211/34874.html