JavaScript Common Mistakes and usage habits (beginners)

Source: Internet
Author: User
Tags variable scope

Data type

1. String

If you want to wrap a string that contains double quotation marks in double quotes, you must escape the double quotes in the string with a backslash:

    var height = "About 5 ' 10\" Tall "; It is important to note that backslashes are not part of a string. You can verify it: alert (height);

2. Arrays and objects

Beginners may use associative arrays, which is not a good practice. You should use a generic object

What is an associative array?

1     varA = Array (3);2A[0]= "My";3a[1]=1995;4a[2]=false;//This is a typical Beatles array. 5 6     varA = Array (3);7A[name]= "My";8a[age]=1995;9a[live]=false;//use strings instead of numeric values, which are called associative arrays to improve readability. 

Since it's all about creating objects for readability, why not add a step array to create, so we can create them directly with objects (object).

1 varA =Object ();//or var a = {};2A.name = "my";3A.age = 1995;4A.live =false;5 //There is also a more concise way of writing, but also the most commonly used notation6 varA = {7Name: "My",8age:1995,9LivefalseTen};

3. Operation

Arithmetic operation method to develop good habits, the combination of various operations, the use of parentheses to separate the operation, to avoid ambiguity.

1+4*5 (1+4)->1+ (4*5)

Sometimes the self-add-write counter (novice) is often reminded again: "+ =".

4. Conditional statements

In the judgment statement if, habitually we will use {}. If the statement is very small, it can be written on the same line.

In the next H5 of the judgment operator, the "comparison operator" needs a more rigorous wording, as far as possible write "= = =", "!==".

There are also our common logic operators && and | | , you should also note that this "logical non" operator such as: "! (Num > | | num < 5) ".

5. Looping statements

A while with an if is more than a loop, and the condition is the same as the loop. There is also a do{}while (), regardless of whether the condition is set up for the first time will be executed.

The most common use for a For loop is to traverse the object length (object.lenght)

var beatles = Array ("John", "Paul", "George", "Ringo");  for var ) {    alert (Beatles[count]);}

6. Scope of functions and variables

Understanding a function is better than understanding its intentions. Let the function be more valuable, as we use it as a data type:

1 functionCONVERTTOUSD ( CNY ) {2     varresult =CNY* 6.5;3     returnresult;4 }5 var CNY= 99;6 varUSD =CONVERTTOUSD ( CNY );7 alert (USD);8 //The result is a conversion of the renminbi into dollars. 

It is worth noting that 1 rows CONVERTTOUSD (CNY) is not equal to 6 rows CONVERTTOUSD (CNY), theCNY is the formal parameter of CNY is the argument

function fn ( CNY ), encapsulating functions in parentheses are formal parameters and are unique. It can be understood as function functions (var CNY ) to define the variables for itself.

Variable scope, the variable that we declare in the function is a local variable that exists only in the context of the function. To confuse the novice with the global and local issues:

1 functionSquare (num) {2Total = num *num;3     returnTotal ;4 }5 varTotal = 50;6 varNumber = Square (20 );7 alert (total);8 //To make the global total unaffected , you can write as follows9 functionSquare (num) {Ten     varTotal = num *num; One     returnTotal ; A } - //This replaces the above, the global and local names, and the local affect the global variables

7.New and constructors

When we add new, we construct the object, and no new is a call to the normal function.

11//when you want to declare several task objects, you may be able to declare a22varNUM1 = {Name:tim, age:20, ... };3   varnum2 = {Name:may, age:19, ... };4   varnum3 = {Name:ken, age:26, ... };5   ... ...6   //one by one statements when the number of a lot, it will be cumbersome, difficult to maintain. 7   //then we have to write a function to construct, that is, new.8   functionPerson (name,age) {9        This. Name =name;Ten       This. Age =Age ; One      ... ... A  } -  varNUM1 =NewPerson ("Tim", 20 ); -  varnum2 =NewPerson ("may", 19 ); the  varNUM3 =NewPerson ("Ken", 26 ); -  ... ... -   -  //If you just want to write a method call, you don't have to new it.  +  //as before the conversion of USD CONVERTTOUSD () is only a method, there is no need to generate objects. 

JavaScript Common Mistakes and usage habits (beginners)

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.