Ecmascript syntax notes

Source: Internet
Author: User

Http://www.w3school.com.cn/js/pro_js_syntax.asp

 

Comparison with C:

 

At the end of each lineSemicolon is optional,BestCodeThe habit of writing is to add semicolons.

 

  • A single line comment starts with a double slash (//)
  • Multi-line comments start with a single slash (/*) and end with a star and a single slash (*/)
  •  

    Declare variables using the VaR operator. ecmascript isWeak type

    Variable declaration is not required

    Ecmascript has another interesting aspect (also with mostProgramThe main difference between design languages) is that you do not have to declare variables before using them. For example:

     
    VaR stest = "hello"; stest2 = stest + "world"; alert (stest2 );

    When the interpreter of ecmascript encounters an undeclared identifier, use this variable name to create a global variable and initialize it to a specified value.The best habit is to always declare all variables like using other programming languages..

    The ECMA-262 defines a set supported by ecmascriptReserved word).

     

    Keywords(Keyword)It is reserved and cannot be used as a variable or function name.

    Reserved Words(Reserved word)In a sense, it is for the future.KeywordsThe reserved word. Therefore, reserved words cannot be used as variable or function names.

     

     

    Ecmascript Original Value and reference value(Important)

    In ecmascript, a variable can have two types of values: the original value and the reference value.

    Original Value
    Simple data segments stored in stacks, that is, their values are directly stored in the variable access location.
    Reference Value
    Objects stored in heap, that is, the value stored in the variable is a pointer to the memory of the storage object.

     

    One of the original types, That is, the undefined, null, Boolean, number, and string types. Because the space occupied by these primitive types is fixed, they can be stored in a small memory area-stack. This storage makes it easy to quickly search for variable values.

    In many languages, strings are considered as reference types rather than original types, because the length of strings is variable. Ecmascript breaks this tradition.

    Ecmascript has five primitive types (Primitive type), That is, undefined, null, Boolean, number, and string.

     

    Undefined type

    As mentioned above, the undefined type has only one value, that is, undefined. When the declared variable is not initialized, the default value of this variable is undefined.

     

    Null type

    Another type with only one value is null. It has only one dedicated value, that is, its literal volume. The undefined value is actually derived from the null value, so ecmascript defines them as equal.

     

    Boolean Type

    The boolean type is one of the most common types in ecmascript. It has two values: true and false (that is, two boolean values ).

     

    Number Type

    The most special type defined in the ECMA-262 is the number type. This type can be a 32-bit integer or a 64-bit floating point number.

    Octal and hexadecimal numbers

    VaR inum = 070; // 070 is 56 in decimal format

    VaR inum = 0x1f; // 0x1f is equal to 31 in decimal format.

    Floating Point Number

    VaR fnum = 5.0;

    VaR fnum = 5.618e7

    8-e17

    Special number value

    Number. max_value and number. min_value

    When the number generated by the calculation is greater than number. max_value, it will be assigned the value number. positive_infinity, meaning no numerical value exists. Similarly, if the generated value is smaller than number. min_value, the value number. negative_infinity is also assigned, meaning that no numeric value exists. If the return value is an infinity value, the generated result cannot be used for other computations.

    In fact, a special value represents infinity (as you can guess), that is, infinity. Number. The value of positive_infinity is infinity. The value of number. negative_infinity is-infinity.

    Since an infinite number can be a positive number or a negative number, you can use a method to determine whether a number is poor (instead of testing each infinite number separately ). The isfinite () method can be called for any number to ensure that the number is not infinite. For example:

     
    VaR iresult = inum * some_really_large_number;If (isfinite (iresult )){Alert ("finite");} else {alert ("infinite ");}

    The last special value is Nan, indicating the non-number (not a number ). Nan is a strange special value. Generally, this occurs when the type (string, Boolean, etc.) Conversion fails. For example, if you want to convert the word blue into a value, it will fail because there is no equivalent value. Like infinity, Nan cannot be used for arithmetic computation. Another strange feature of Nan is that it is not equal to itself, which means that the following code will return false:

     
    Alert (Nan = Nan); // output "false"

    For this reason, the Nan value itself is not recommended. The function isnan () will do quite well:

     
    Alert (isnan ("blue"); // output "true" alert (isnan ("666"); // output "false"

     

     

    String type

    The unique feature of the string type is that it is the only primitive type with no fixed size. You can use strings to store 0 or more Unicode characters, represented by 16-bit integers (Unicode is an international character set, which will be discussed later in this tutorial ).

    The string literal is declared by double quotation marks (") or single quotation marks. Java uses double quotes to declare strings and single quotes to declare characters. However, because ecmascript does not have a character type, either of these two representations can be used. For example, the following two lines of code are valid:

    VaR scolor1 = "red"; var scolor2 = 'red ';

     

    Ecmascript type conversion

    VaR inum1 = parseint ("12345red"); // 12345 is returned.

    VaR inum1 = parseint ("0xa"); // return 10

    VaR inum1 = parseint ("56.9"); // return 56

    VaR inum1 = parseint ("red"); // return Nan

     

     

     

     

    All attributes and methods of string objects can be applied to the original values of string because they are pseudo objects.

     

    Todo:

    Http://www.w3school.com.cn/js/pro_js_operators_unary.asp

    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.