Learn Blus teacher JS (2)--Deep JavaScript

Source: Internet
Author: User
Tags array sort

1. Function parameters

variable parameter (indeterminate parameter): variable number of arguments parameters, example of parameter array 1. Sum of all parameters
<! DOCTYPE html>functionsum () {//alert (arguments.length);    //alert (arguments[0]);    //arguments        varResult=0;  for(vari=0;i<arguments.length;i++) {result+=Arguments[i]; }        returnresult;} Alert (SUM (12, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6));</script>View CodeExample 2:css function to judge arguments.length parameter name, enhance readability
<! DOCTYPE html>functioncss (obj, name, value) {if(arguments.length==2)//Get    {        returnObj.style[name]; }    Else                    //Set{Obj.style[name]=value; }}window.onload=function (){    varOdiv=document.getelementbyid (' Div1 '); Alert (CSS (Odiv,' Width ')); //css (odiv, ' background ', ' green ');};</script>View Code

2. Take non-inline styles:

Take a non-inline style (cannot be used to set):

OBJ.CURRENTSTYLE[ATTR]//iegetcomputedstyle (obj, false) [attr]//chrome/ff
<! DOCTYPE html>#div1 {width:200px; height:200px; background:red;}</style><script>functionGetStyle (obj, name) {if(Obj.currentstyle)//IE    {        returnObj.currentstyle[name]; }    Else                                    //Chrome/ff    {        returngetComputedStyle (obj,false) [name]; }}window.onload=function (){    varOdiv=document.getelementbyid (' Div1 '); Alert (GetStyle (Odiv,' BackgroundColor '));};</script>View Code

But pay attention to the above getstyle () problems, this can only take a single style, if you want to take a compound style, such as the above background, to use the ' backgroundcolor '

3. Array Basics

1) adding, deleting elements

Add to
    • Push (Element), added from tail
    • Unshift (Element), added from the head
Delete
    • Pop (), pops up from the tail
    • Shift (), pops up from the head

2) Insert, delete

Splicesplice (start, length, element ...) First Delete, then insert
    • Delete
Splice (start, length)
    • Insert
Splice (start, 0, element ...)
    • Replace
<script>var arr=[1,2,3,4,5,6]; // Delete: Splice (start, length) // Arr.splice (2, 3); // Insert: Splice (beginning, length, element ...); // Arr.splice (2, 0, ' A ', ' B ', ' C '); Arr.splice (2, 2, ' A ', ' B '); alert (arr); </script>
View Code

3) Sort, convert

Sort
    • Sort ([comparison function]), sorting an array
Sort a string array sort a numeric array transformation class
    • Concat (array 2)
Connect two arrays
    • Join (delimiter)
Use delimiters, combine array elements, generate string string split
<script>var arr=[' float ', ' width ', ' alpha ', ' zoom ', ' left '];arr.sort (arr) ; </script>
View Code
<script>var , 8, arr=[12,];arr.sort (  N1, n2) {      return n1-n2;     /* if (n1<n2)    {        return-1;    }    else if (n1>n2)    {        return 1;    }    else    {        return 0;    } */ }); alert (arr); </script>
View Code

4.

----------

Learn Blus teacher JS (2)--Deep JavaScript

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.