10 JavaScript experience per day (i) basic knowledge

Source: Internet
Author: User

1. Do not use the script self-closing label

A closed tag is used in script, although he is legitimate in XHTML, but does not conform to the HTML specification and does not have the correct parsing of some browsers. I used to use this approach when I introduced Ext, causing the script to not execute correctly.

<script src="example.js"/> --> <script src="example.js"></script>

2. Put the script in front of </body>

If you put the script file in

<! DOCTYPE html>
 
 

3. Use strict mode within a function

If you use strict mode outside of a function, it is possible that third party libraries, and colleagues ' code will not work properly, within the function can only affect their own code, not to affect other people's code.

function MyFunction () {
 "use strict";
 function Code
}

4. Do not omit the semicolon at the end of the statement

No semicolon at the end of the code causes a compression error, and in some cases improves the performance of the code, which should be no more than the time it takes to speculate on where to insert the semicolon. A more common problem is that automatic insertion of semicolons can sometimes be an error, so it is not recommended to omit semicolons.

5. Define variables using var

The var keyword is used when defining variables, and all advance to the beginning of the function.
The benefits of doing so will avoid creating global variables unconsciously and making your code easier to understand.

function MyFunction () {
 var result = ten + value;
 var value = ten;
 return result;
}

This function is grammatically correct, but it is not intuitive and does not conform to human logic, and is better modified as follows:

 Funciton MyFunction () {
  var result;
  var value;
  result = ten + value;
  Value = ten;
  return result;
 }

To explain that, the above two code is equivalent, the value of result is nan.javascript will be the function of all the variable declarations to the beginning of the function, the code in the execution of code will become code two, when run to result = ten + value; Value is undefined, and 10 is added to Nan, and value is assigned to 10.

Regarding the global variable brings the question, everybody must also be quite clear, otherwise also does not appear the namespace concept.

6. function first declares and then uses

As with variable declarations, function declarations are also advanced by the JavaScript engine, so in code, function calls can appear before the function's declaration. It is also noteworthy that a function declaration should not appear within a statement block, such as:

if (condition) {
 function MyFunction () {
  alert ("true");
 }
} else{
 function MyFunction () {
  alert ("false");
 }
MyFunction ();

Running the code we found that the output would be related to the browser and output false under Chrome 51 and Firefox 46 output True,ie 10. So try to avoid declaring the function in the statement block.

7. Careful use of typeof underfined null judgment

Null is a special value that we often confuse with undefined, and the following scenarios should use NULL:

    1. Used to initialize a variable that may be assigned to an object.
    2. Used to compare with a variable that has already been initialized.
    3. Used as a parameter pass when the parameters of the function expect to pass in the object.
    4. Used as the return value when the return value of the function is expected to be an object.

Null should not be used in some of the following situations:

    1. Do not use NULL to check whether a parameter has been passed in.
    2. Do not use NULL to check whether a variable is initialized.

The best way to understand Null is to use him as a placeholder for an object. We often confuse null with undefined because we think null and undefined are variables uninitialized, but only undefin represents a variable that has not been initialized, and null represents initialization as an object. Look at the following code:

var person;
Console.log (typeof person);  Undefined
console.log (typeof foo);   Undefined
var house = null;
Console.log (typeof house);  Object

So try not to use TypeOf to determine whether a variable is initialized, you can not determine if the variable does not exist or the variable is not initialized, return null is you can not determine if the variable is correctly assigned, so be careful to use the TypeOf.

8. Use the number type carefully

I guess everyone knows that JavaScript integers support decimal, octal, and hexadecimal literal values. In octal if the values in the literal value exceed the range, the leading 0 is ignored and the following values are parsed as decimal.

console.log(012);    //10
console.lgo(082);    //82

If you use octal and hexadecimal for decimals, you will get a syntax error. Also, the octal literal is not valid in strict mode. On the problem of the calculation error of floating-point number It is also clear that all floating-point computations based on IEEE754 values are the same, so never test a particular floating-point value.
There is a special value in the numeric type, NaN (not a number), which indicates that the value should have been returned but not the numeric type. Nan and any values are not equal, including the Nan itself. We can test with the isNaN () function.

9. Using logical operations to dynamically assign values

We prefer the operation

var person={
  age:10
}
var condition;
var MyVar = condition && person;
Alert (MyVar)

If condition converts to Boolean type false, MyVar = condition, if true, MyVar = person.

var person={
    age:10
  }
var condition;
var MyVar = Condition | | person;
  Alert (MyVar)

If condition converts to Boolean true, MyVar = condition, or false, MyVar = person.

10. Do not use the WITH statement

One important reason for not using with is that the syntax itself disables the WITH statement in strict mode, which also indicates that the ECMAScript Committee is convinced that it should not be used with. We look at the following examples:

var book = {
 title: "Maintainable JavaScript",
 Author: "Nicholas C. Zakas"
};
var message = "The book is";
With (book) {message
 = = title;
 Message + = "by" + Author;
}

The problem with the above code is that it's hard to tell where the title and author appear, whether it's a local variable or a property of book, and the JavaScript engine and the compression tool can't optimize the code. They should not be able to guess the correct meaning of the code.

OK, it's 10, we'll see you next 10.

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.