JavaScript Seven Tips (i) _javascript skills

Source: Internet
Author: User

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.

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.