JavaScript notes-Shortage make 2-reference types

Source: Internet
Author: User

  A reference type is a data structure used to organize data and functionality together. It describes the properties and methods that a class of objects have. object is an underlying type, array is of type, date is of type, regexp is a regular expression type, and so on.

  1. Object type
    1. Create:
      var New Object ();

      Often used to store and transfer data. such as storage:

      var New Object ();         = "Nicholas";         = 29;

      Second way to create: (when created, the property name can also be a string format, that is, you can enclose the property name in quotation marks.) )

      var person = {            "Nicholas",            +        };

    2. Remove attribute value: person["name"]; or: person.name;
  2. Array type
  3. The same array can hold any type of data (a hodgepodge).
    1. Arrays can be dynamically adjusted (adding a single data, it grows by itself a length, not dead.) )。
    2. Create:
      var stars=New Array (); // Mode 1 var stars=New Array (20); // Mode 2 var stars=New Array ("Jay Chou", "JJ Lin", "Stefanie Sun"); // Mode 3 var stars=array (20); // Mode 4 var stars=["Jay Chou", "Stefanie Sun", "JJ Lin"; // Mode 6

    3. Example of dynamic adjustment:
      var stars=["Jay Chou", "JJ Lin", "Stefanie Sun"];stars[1]= "JJ"; // Dynamic Change (turn JJ Lin into JJ)stars[3]= "Leather pants Wang"; // dynamic growth (added a length)stars.length=1; // Dynamic Force reduction (JJ Lin, Stefanie Sun, leather pants Wang forced removal, length changed to 1)

    4. Detection array: Array.isarray (value);
    5. Use Join () to convert the array to a delimited string:
      var stars = ["Jay Chou", "Wang Nima", "Zhangquan egg"];        Alert (stars Join (","));      // Jay Chou, Wang Nima, Zhangquan egg        Alert (stars Join ("-"));     // Jay Chou-Wang Nima-Zhangquan Egg

    6. You can use an array like a stack (pop () out, push () to go in).
    7. Arrays can be used like queues. (Combine shift () and push ()):
      varStars =NewArray ();//Create an array        varCount = Colors.push ("Jay Chou", "Wang Nima");//Push the itemsalert (count);//2Count= stars. Push ("Zhangquan egg");//push another item onalert (count);//3                varitem = Colors.shift ();//get The first itemalert (item);//Jay Choualert (colors.length);//2/** The so-called stack change queue, is actually the stack upside down and then pull*/

    8. Sort.
      1. Reverse () flips the array order (returns the sorted array)
      2. Sort () from small to large. But sort by string, not by number: (returns the sorted array).
        1  var values = [0, 1, 5, ten, +]; 2         Values.sort (); 3         alert (values);    // 0,1,10,15,5

        To sort by the way you want, you can add a comparison function to the sort () as a parameter:

        functionCompare (value1, value2) {if(Value1 <value2) {                return-1; } Else if(Value1 >value2) {                return1; } Else {                return0; }        }                varvalues = [0, 1, 5, 10, 15];        Values.sort (Compare);    alert (values); //0,1,5,10,15

        A simplified version of the comparison function (sort only cares whether the returned positive, negative, or 0):

        function Compare (value1,value2) {  return value2- value1;  }

    9. Operation of an array: junction, Slice, splice.
      1. Junction: Use Concat, Memory: concat-->concatenate: Link, chain. Example:
        var stars = ["Jay Chou", "Wang Nima", "Zhangquan egg"]        ; var stars 2= stars concat ("Crown Princess", ["Flower Thousand Bones", "lists"]);                Alert (stars);      // Jay Chou, Wang Nima, Zhangquan      egg        alert (stars2);    // Jay Chou, Wang Nima, Zhangquan Egg, Crown Princess, Flower thousand bones, lists

      2. Slice. Using Slice, Memory: Slice translation: slices. Example:
        var stars = ["Lists", "King of Fame", "Jing Wang", "Neon", "Liu Fei")        ; var stars2= stars.slice (1);         var stars3= stars.slice (1,4);                alert (stars2);    // Yu Wang, Jing Wang, Neon, Liu Fei (starting from the first position to cut)        alert (STARS3);   // Yu Wang, Jing Wang, neon (from 1th position to 3rd position, 4 for semi-enclosed, not included)

      3. Stitching. Splice Powerful. can be deleted, inserted, replaced.
        1. Delete any number of items: for example: Splice (0,2), delete No. 0, 1 (semi-enclosing interval) (return delete item).
        2. Insert any number of items at the specified location: for example: Splice (2,0, "Jay Chou", "Wang Nima"), starting from the 2nd position to insert Jay Chou, Wang Nima two.
        3. Inserts any number of items at the specified location and deletes any number of items at the same time. For example: Splice (2,1, "Jay Chou", "Wang Nima"), remove 1 items from the 2nd position, and then start inserting Jay Chou, Wang Nima two.
    10. Location method: Indexof,lastindexof;
    11. Iterative method: Divided into: All qualified to pass, any pass through, filter some slag slag, one-to-one mapping, iterative query, reduce.
      1. All qualified only through:
        var numbers = [1,2,3,4,5,4,3,2,1];                 var everyresult = Numbers.every (function(item, index, array) {            return (item > 2);        });                alert (everyresult);        // false

        Each item in the previous example is greater than 2 to return true.

      2. any one pass through:
         var  numbers = [1,2,3,4,5,4,3,2,1                 var  Someresult = numbers.some (function   (item, index, array) { return  (        Item > 2);                });       alert (Someresult);  // true  

        In the previous example, There is a value greater than 2 to return true.

      3. Filter Some dross:
         var  numbers = [1,2,3,4,5,4,3,2,1                 var  Filterresult = numbers.filter ( function   return<        /span> (item > 2);                });   alert (Filterresult);  // [3,4,5,4,3]  

        In the previous example, Filter out more than 2.

      4. One-to-one mapping:
        var numbers = [1,2,3,4,5,4,3,2,1];                 var mapresult = Numbers.map (function(item, index, array) {            return item * 2;        });                alert (mapresult);    // [2,4,6,8,10,8,6,4,2]

        In the example above, multiply each item by 2.

      5. Iteration: Using For-each.
      6. reduction: Reduce.
         var  values = [1,2,3,4,5 var  sum = values.reduce (function   (prev, cur, index, array) { return  Pre        V + cur;        }); alert (sum);  // 15  
        The

        Summation sum is returned, and 5 items are reduced to 1 items.

  4. regexp type
    1. var expression=/pattern/flags;
    2. The
    3. flags are three kinds: g (global universal mode, applies to all strings), I (case-insensive, ignore alphabetic case), M (multiline, multiline mode, one line is checked and the next line is finished). )。 Example:
       /*  * matches all instances of ' at ' in the string  */ var  pattern1=/at/G;  /*  * matches the first ' bat ' or ' cat ', not case-insensitive  */ var  pattern2 =/[bc]at/i;  /*  * matches all 3-character combinations ending with ' at ', not case-sensitive  */ var  pattern3=/.at/gi; 

       

    4. All meta characters in the
    5. pattern must be escaped, metacharacters: ({[\ ^ $ |)? * +.]}
  5. function type
    1. Each function is an instance of a type of functions, and as with other reference types, All have properties and methods. Two ways to define the
    2. function: Method 1:
       function   sum (A, B  { return  a + b;  

      Method 2:

       var  sum= function   (A, b) { return  a + b;}  

       

    3. The
    4. function has no overloads.
  6. Boolean, Number, String: Basic wrapper type
    1. var a= "Jay Chou is a superstar"; var b=a.substring (0,8);

      In the above example, A is a basic type, but a can call the substring method because the background automatically completes a wrapper operation, creating an instance of string. Boolean,number is similar.

  7. Monomer built-in object, not need to instantiate, directly use, such as: Math,global.
    1. Functions and variables defined in all global scopes are methods of the global object, such as: Parseint,isnan, and so on.
    2. The eval () method is also a method of the global object, which is responsible for parsing JavaScript.
    3. The math object is to hold mathematical formulas and related information. It has a number of methods, such as: Min to find the minimum, max for Max, Ceil () up the rounding up, floor down rounding, round rounding, random number.

JavaScript notes-Shortage make 2-reference types

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.