Javascript you don't know about.

Source: Internet
Author: User
Tags case statement

    1. Nan represents a result of an operation that does not produce normal results. It does not equal any value, including its own. Can be detected with isNaN (number).
    2. Like a string in Java, the string in JS is immutable. This means that once a string is created, it cannot be changed. Subscript expressions (such as s[3], instead of S.charat (3), can read characters, but cannot be used to change characters.
    3. null,undefined, the empty string ", 0,nan is treated as false, the rest of the value (including" false ") or the object is treated as true. The so-called "being treated" is not to say that these values are Boolean true, such as true== "false" does not hold, but rather in conditional judgment statements such as if ("false") alert (' "false" judge as true '); This occurs because a non-empty string or non-null object is equivalent to!undefined or!null, and its value is treated as true, which is natural. We can use two non-sign "!" Casts a value to a Boolean type, visually viewing it as true or false, such as alert (!!! False "), output true.
    4. Unlike the C + +, Java, and other languages, JavaScript switch statements can not only use numbers, characters to do conditional expressions, you can also use strings! In fact, JavaScript does not have the basic type of characters, and a single character is also a string. Even more powerful, the expression of a case statement can be a variable! Here's an example:
      [JavaScript]View Plaincopyprint?
      1. function T (p,v) {
      2. switch (p) {
      3. case ' C ':
      4. Alert (' char ');   Break
      5. case ' string ':
      6. Alert (' string ');   Break
      7. Case V:alert (' variable: ' + P);   Break
      8. Default:alert (P);
      9. }
      10. }
      11. T (' C '); ' Char '
      12. T (' string '); ' String '
      13. T (123); //' 123 '
      14. T (123,123); //' variable:123 '
    5. The program can throw an exception at any time, throw out can be any expression, object or constant; Of course, a good exception handling system needs to be carefully designed.
    6. The switch, while, do, and for statements allow a predecessor tag to be used with a break or continue statement to complete some jumps.
    7. A JavaScript object is a mutable keyed collection (keyed collections), a container for properties, a property name that can even be an empty string "", and a property value can be any value other than undefined. Simple types (number,string,boolean,null,undefined) seem to be objects because they have methods, but they are immutable.
    8. Delete Deletes the object's properties, does not touch the object in the prototype chain, and returns False when the Delete fails. Global functions and variable definitions are also properties of global objects and can be deleted with a delete operation, while variable declarations and function definitions inside functions cannot be deleted, for example, the result of the following function execution is a popup "false".
      [JavaScript] view Plaincopyprint?
      1. function f () {
      2. var a = 3;
      3. Alert (delete a);
      4. }
    9. The fromCharCode method of string returns a string from a string of numbers representing ASCII code, which may be useful in some cases.
      var a = String.fromCharCode (67,97,116);
      A is ' Cat '

    10. The first argument to the replace () method of string is a string or a regular expression. The second argument, if it is a string, can specify information such as the matching grouping of regular expressions, such as $ A for the third capturing group. The second argument can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text.
      The function's arguments are similar to the array returned by the regular expression exec method: The first argument is a string that matches the pattern, the next argument is a string that matches the subexpression in the pattern, can have 0 or more such arguments, and then the argument is an integer that declares where the match appears in the original string. , the last parameter is the entire string. Examples are the most powerful means of instruction:

      [JavaScript]View Plaincopyprint?
        1. var s = ' FON230NVA09DNFADSF ';
        2. var r =/(\d{2}) ([a-za-z])/g;
        3. var t = s.replace (R,function () {
        4. /*......*/
        5. });
      In the corresponding first substitution, the contents of the arguments are:
      ARGUMENTS[5]
        1. 0: "30n"
        2. 1: "the "
        3. 2: "n"
        4. 3: 4
        5. 4: "FON230NVA09DNFADSF"
      The return result of the Exec method is a JS array that comes with two additional attributes index and input. The return results for the first execution of the Exec method corresponding to the above replace are as follows:
      [JavaScript]View Plaincopyprint?
        1. R.exec (s);
      ARRAY[3]
        1. 0:   "30n"
        2. 1:  ""
        3. 2: " n "
        4. index : 4
        5. input:  "FON230NVA09DNFADSF"
        6. length:  3
    11. The match method of string determines how the match is done based on the G-ID. If there is no G-ID, the result of calling String.match (RegExp) is the same as the result of the first call to Regexp.exec (string). If the regexp has a G-ID, it returns an array containing all the matched strings.
      [JavaScript]View Plaincopyprint?
        1. var s = ' FON230NVA09DNFADSF ';
        2. var r =/(\d{2}) ([a-za-z])/;
        3. S.match (R);
      Output result: array[3]
        1. 0: "30n"
        2. 1: "the "
        3. 2: "n"
        4. Index: 4
        5. Input: "FON230NVA09DNFADSF"
        6. Length: 3
      [JavaScript]View Plaincopyprint?
        1. var r =/(\d{2}) ([a-za-z])/g;
        2. S.match (R);
      Output result: array[2]
        1. 0: "30n"
        2. 1: "09d"
        3. Length: 2
    12. The split (Separator,limit) method of string splits a string into fragments to create an array of strings. The optional parameter limit limits the number of fragments to be split. The separator parameter can be a string or a regular expression, and the text from the grouped capture will be included in the segmented array. A comprehensive example is given below:[JavaScript] view Plaincopyprint?
      1. var s = ' str1, str2, Str3 '; S.split (/\s (,) \s/,4);
      2. Output results: ["str1", ",", "str2", ","]
    13. The substring function of string has the same usage as slice, but it cannot handle negative arguments. The Substr method is not standardized in ECMAScript, so if you want to take a substring of a string, use the slice Method!
    14. If a regular Expression object, Reg, has a G ID, the Exec method's lookup does not start at the beginning of the string, but starts at Reg.lastindex, and resets it to 0 if it encounters an unsuccessful match. The ^ factor also matches only the case where lastindex is 0, so in some cases it may be necessary to manually reset the lastindex to 0!
    15. Do not use the G-ID for the test method of the regular expression, because it may call exec at the bottom to determine that the test method will have an unnecessary dependency on the Lastindex property of the regular expression.
    16. An escaped point number "." Any characters except the line terminator will be matched.
    17. In a regular expression, (?: EXP) represents a non-capturing group that does not interfere with the number of a capturing type grouping. It is a good way to replace an inappropriate capturing type group with a non-capturing group, which reduces the performance penalty. and (?: Exp)? represents an optional non-capturing grouping because the "? "Represents a repeat of 0 or 1 times, so the non-capturing group is optional.
      You should avoid using the/* */Comment because it may conflict with regular expression literals.
    18. The array of JS can be accessed by a numeric subscript in the form of a string, because the elements of the array are actually properties of the array object. Delete can be used to remove elements from an array, but because an element is an attribute, delete leaves a "hole" in the array; using the splice method may be a better approach. For example, to delete the 3rd element of the ARR array, you can use the statement: Arr.splice (2,1), but for large arrays, the function may not be efficient.
    19. The length property of an array is not enumerable.
    20. For the effect of parentheses on scopes, see the following example:
      [JavaScript]View Plaincopyprint?
      1. var x=10;
      2. var foo = {
      3. X:20,
      4. Bar:function () {
      5. var x = 30;
      6. return this.x;
      7. }
      8. }
      9. var a = (Foo.bar) (); //parentheses do not affect the value of the expression, which is equivalent to calling Foo.bar () directly
      10. var B = (Foo.bar=foo.bar) ();  The //Bracket expression Returns the result of the assignment operation, which is the member bar function of Foo, and the call is returned to the global scope.
      11. var c = (Foo.bar,foo.bar) ();  //Is the same as the above, which is the result of the return expression, that is, the bar function later, which is equivalent to calling the bar function at the global scope.
      So the values of a, B, and C are 20,10,10 respectively.

Javascript things you don't know

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.