What are the seven details that JavaScript beginners must pay attention to?
This article will explain in detail what are the seven details that beginners must pay attention to in JavaScript. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
(1) simplify the code
JavaScript is very simple to define objects and arrays. We want to create an object, which is generally written like this:
Var car = new Object (); car.colour = 'red'; car.wheels = 4; car.hubcaps =' spinning'; car.age = 4
The following writing methods can achieve the same effect:
Var car = {colour:'red', wheels:4, hubcaps:'spinning', age:4}
The latter is much shorter, and you don't have to repeat the object name.
In addition, there is also a concise way to write arrays. In the past, we declared that arrays were written like this:
Var moviesThatNeedBetterWriters = new Array ('Transformers','Transformers2','Avatar','Indiana Jones 4')
A more concise way of writing is:
Var moviesThatNeedBetterWriters = ['Transformers','Transformers2','Avatar','Indiana Jones 4']
For arrays, there is also something special about concatenated arrays. You will find that a lot of code defines objects like this:
Var car = new Array (); car ['colour'] =' red'; car ['wheels'] = 4; car [' hubcaps'] = 'spinning'; car [' age'] = 4
This is crazy, don't be confused, "associative array" is just an alias for the object.
Another way to simplify code is to use ternary operators, for example:
Var direction; if (x < 200) {direction = 1;} else {direction =-1;}
We can replace this with the following code:
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, and you can use native javascript methods to store complex data without any additional transformations, such as:
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 API, which is used in many API, such as:
Function delicious (o) {var out ='; for (var item0)