Seven precautions for learning JavaScript: _ basic knowledge-js tutorial

Source: Internet
Author: User
The following small series will bring you one of the seven considerations for learning JavaScript ]. I think this is quite good. I will share it with you and give you a reference. Knowledge description:

For beginners of JavaScript, pay attention to the following seven major details to make our code easier to understand and more efficient when the same function is implemented.

I. Simplified code

Example: Create an object

Previously, this was the case:

Var car = new object ();

Car. color = "red ";

Car. wheels = 4;

Car. age = 8;

But now we can write it like this:

Var car = {color: 'red', wheels: 4, age: 8}

Example: Create an array

Previously, this was the case:

Var studentArray = new Array ('hangsan', 'lisi', 'zhaowu', 'wuliu ');

But now we can write it like this:

Var studentArray = {'zhangsan', 'lisi', 'zhaowu', 'wuliu '};

For example, use a ternary operator to simplify code

The preceding statement is as follows:

Var result;if(x > 100){    Result = 1;}else{    Result = -1;}

Now you can write it as follows:

Var result = x> 100? 1:-1;

Ii. Use JSON as the data format

Use Json format to store data:

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 also use JS to store data. The Code is as follows:

《script》 Function saveData(data){ Var out =“
    ”; For(var i=0; i +data[i].d+”;}Out += ‘
';Document.getElementById(‘dataDiv').innerHTML = out;}《script》

You can even use the stored data generated by JS as the API return value.

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.