JavaScript Basics-Episode

Source: Internet
Author: User

JavaScript basics

In 1:js we'd better use single quotes, but we can use double quotes, but in order to make a difference, JS uses single quotes. Comments are the same as in C #. The order of execution in the Web page is executed from top to bottom, regardless of where you place the JS, it will be executed in order. If there is an error in JS, just do not continue to execute this JS in the <script></script> code, but the rest of the Web page JS code will still be executed.

2: When we write JS code, we must add a semicolon, this advantage has two: 1, can be easily compressed, 2, improve the readability of the code, maintainability.

The data type in the 3:JS.

We can use typeof () to get the data type of the object.

The difference between = = and = = in 4:js

The two of them have a common point, if the data type is consistent, to judge, not the same = = will be converted in judgment.

= =: It is our most common equal sign, but in JS will be two different data types of values, conversion, if the conversion, the two are equal.

= = = This is an absolute equals, as long as the data class row is different, that is unequal.

null and undefined in 5:JS

JS is generally used before using variables should be judged whether they are available, and then in use. Declare the variable is not assigned value, then this variable is undefined, the variable is not declared, then the browser will error. When the method does not return a value, it receives the undefined. The null value is actually a value in the variable, and the value is "null". variables, both null and undefined, indicate that variables are not available, so we have to make judgments when we use them and check whether they are usable.

Variable field of 6:JS

The variables declared in *:JS are available throughout the page, and this C # is different. But the variable declared in the function has its own scope.

*: If the var keyword is not written when declaring a variable, the variable is a variable that can be accessed by an entire page.

Conversion issues in 7:JS

à if the conversion pops up Nan, it is a double type value.

à parseint (): This method converts a variable to an int type, which is the first digit in the string, and if a number is found for conversion, a few conversions are found. This is the way of metamorphosis. The default is 10 binary.

à

    1. <script type= "text/javascript" >
    2. ???? var n = parseint (' a1b2v4 ');
    3. ???? if (IsNaN (n)) {
    4. ???????? Alert (' failed ');
    5. ????} Else {
    6. ???????? Alert (' success ');
    7. ????}
    8. </script>

About Nan, do not use = =, this does not work here, equivalent to not write.

8: About the eval () function

This method is a string as the JS code to execute the method, but we do not write in the JS code, he will be output.

9:&& (and), | | Or

&&: a false to false;

| |: A true is true; this is the same as in C #. In JS we often use two elements to compare, here Note 0 is false, 1 is true.

Commissioning of the 9:js

Using Chorme for debugging, the interface is as follows,

1>: Click Sources to enter the address of the found file.

2>: see where to go in the JS card breakpoint, directly click on the previous,< here and VS >.

3>: F5 refresh, let the program execute, then the page will be stuck in the place where you set the breakpoint, on the right there will be a choice to debug the way, you can listen, such as each individual variable. This is the right place to debug.

10:js there is no overloaded argument, this is because our second declared function overwrites the first declared function, so only the last declared function is preserved in the Web page.

à JS in the function does not need to define formal parameters, the purpose of defining formal parameters is for the convenience of use. We can pass multiple arguments to the following function.

  1. <script type= "text/javascript" >
  2. ???? function F1 () {
  3. ???????? Alert (' Hello World ');
  4. ???????? var sum = 0;
  5. ???????? for (var i = 0; i < arguments. length; i++) {
  6. ???????????? sum = sum + arguments[i];
  7. ????????}
  8. ???????? Alert (sum);
  9. ????}
  10. ???? F1 ();
  11. ???? F1 (1,1,2);
  12. ???? F1 (1,24,56);
  13. </script>

This is not overloaded for a reason, because it has a arguments variable in the function, in fact, this can be said to be a number of groups, each time we pass in the values are saved here, we can directly in the function to use this to take the value.

à when the code is executed from the top to the bottom of the Web page, the function is pre-parsed, so that the function's overwrite occurs. Variables are also pre-parsed, both in the whole and in the local. Moreover, the pre-parsing of variables is only an analytic declaration but does not parse the assigned value. When writing the JS code, it is best to declare it all first, thus reducing the pre-parsing.

à The function in JS is the object, and the ToString () method of the JS object is to output it as is. Length is the number of its function parameters.

11: Anonymous function

    1. var fff = function (a) {
    2. ???? Alert (' FAI ');
    3. };
    4. FFF = "+";
    5. alert (FFF);
    6. ?
    7. Alert (FFF (1));

Second, the anonymous function is called at the same time as the method is written. This is a real pervert, not a good understanding.

(function (x, y) {alert (x + y);}) (1, 2);

The benefit of this writing reduces the scope of the variable name in the code.

The third type:

The anonymous function is defined by the new function (), the function code can be represented by a string, and the variable used in the function will not appear as "all variables" of the page, and cannot be accessed outside the function, unlike eval ().

    1. var f = new Function (' x ', ' y ', ' z ', ' Alert (x+y+z) ');
    2. var f = new Function (' x, y, z ', ' Alert (x+y+z) ');

JavaScript Basics-Episode

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.