JavaScript is a very popular programming language, many developers will choose JavaScript as an introductory language, this article to share JavaScript seven practical skills, best practices, and other very useful content. In the past, if you want to create an object, you need to do this:
var car = new Object ();
Car.colour = ' red ';
Car.wheels =;
Car.hubcaps = ' spinning ';
Car.age =; The following writing can achieve the same effect: var car = {
colour: ' Red ',
wheels:,
hubcaps: ' Spinning ', age
:
}
Much simpler, you don't have to use the name of this object over and over again.
So the car is defined, maybe you will encounter invaliduserinsession problem, this is only you in the use of IE will encounter, just remember a little, not the right big
You won't get into trouble if you write a semicolon in front of the number.
Another handy shorthand is for arrays.
The traditional way of defining arrays is this:
var moviesthatneedbetterwriters
= new Array (
' Transformers ', ' Transformers ', ' Avatar ', ' Indiana
Jones '
); The shorthand version is this: var moviesthatneedbetterwriters
= [
' Transformers ', ' Transformers ', ' Avatar ', ' Indiana
Jones ' '
];
For arrays, there is a problem, but there is no graph group function. But you'll often find someone who defines the car on top, like this.
var car = new Array ();
car[' colour '] = ' red ';
Car[' wheels '] =;
car[' hubcaps '] = ' spinning ';
Car[' age '] = =;
arrays are not omnipotent; it's confusing to write wrong. The graph group is actually the function of the object, people confuse these two concepts.
Another very cool shorthand method is to use the ternary conditional notation.
You don't have to write the following ...
var direction;
if (x <) {
direction =;
else {
direction =-;
}
...
You can use the ternary conditional notation to simplify it:
var direction
= x < 200? 1:1;
When the condition is true, take the value following the question mark, or the value after the colon.
This is the cloud Habitat Community Small series for you to share about the seven skills of JavaScript (i), the follow-up to update your JavaScript seven tips (b), we will also update more on this knowledge, hope to be helpful to everyone.