JavaScript Basic Note Two

Source: Internet
Author: User

First, function return value
1. What is a function return value
The execution result of the function
2. You can return undefined without return//no return or null after return
3. A function should return only one type of value

Two, variable parameter (indefinite parameter) arguments

function sum (A, b    ) {var result=0;     var  I=0;      for (i=0;i<arguments.length;i++)    {    result+ =arguments (i);    } alert (resulet);} SUM (12,5,7,5,5,6,7,3,7)

Case: Take non-inline styles

Knowledge required for the case

Because using the style directly only gets the style defined within the row, the non-inline style requires the following properties-get the computed style (current/final style)

Currentstyle//ie available, cannot get compound style

Available under getComputedStyle ()//ff

Styles can be divided into:

Basic styles such as: width

Composite styles such as: background//With Color,posiition,repeat,image properties

Simple compatibility is as follows:

Odiv=getelementbyid ("Div1");
if (Odiv.currentstyle)  {      //IE      alert (oDiv.currentStyle.width);  }   Else   {      //FF      alert (getComputedStyle (Odiv,false). width);}

Simple function Encapsulation:

function   GetStyle (obj,attr) {    if(Obj.currentstyle)    {        return  OBJ.CURRENTSTYLE[ATTR];    }     Else     {        return getcomputedstyle (obj,false) [attr];    }}

Third, Array basics
1. How to define
Method one var arr=[1,2,3,4];

Method two var new Array (1,2,3,4);

No difference general use method one code short

2. Properties of an array
Length
Both can be obtained, and can be set
Example: Quickly emptying an array: arr.length=0;

3. Array usage principles
Arrays should have only one type of variable

Iv. Common methods of arrays
1. Add
Push (Element) trailing add Arr.push (5)
Unshif (Element) header add

2. Delete
Pop () pops up from the tail
Shift () pops up from the head

3. Sorting
Sort ([comparison function]) sorts an array

(1) Sort string (default sort string)

var arr1[' small ', ' hello ', ' you ', ' big ']; var arr2[34,100,98,4];arr1.sort (); Arr2.sort (); alert (arr1); alert (ARR2) ;

Results: big,hello,small,you
100,34,4,98

(2) Sort numbers

Arr2.sort (functionreturn num1-num2// Small to large, num2-num1});

Results: 4,34,98,100

4. Conversion class
(1) Concat connection array

var arr1[1,2,3,4];
var arr2[5,6,7,8];

Alert (Arr1.concat (ARR2));

Results: 1,2,3,4,5,6,7,8

(2) Join (delimiter)
Use separators to combine array elements to generate strings

Alert (Arr1.join ("-"));  

Results: 1-2-3-4

(3) split is opposite to join

var arr3= '12-5-7-8 ' var arr3=str.split ('-'); alert (arr3[0]);

Results: 12

5. Insert, delete

Splice (starting position, length, Element)

var arr[1,2,3,4,5,6];

(1) Intermediate Delete

Arr.splice (2,3)

Results: 126

(2) Intermediate insertion

Arr.splice (5,0, ' A ', ' B ', ' C ')

Results: 123456ABC

(3) Intermediate replacement

Arr.splice (0,2, ' A ', ' B ', ' C ')

Results: ab345

Principle: Delete and insert first

Learn notes, mistakes please correct ~

JavaScript Basic Note Two

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.