03. In-depth javascript

Source: Internet
Author: User
Tags array definition

function return value

The function return value is the opposite of the function argument, and the function parameter is that we can pass something into the function, and the function return value is the function that can transfer something to the outside.

<script>
Function Show ()
{
return 12;
}
Alert (Show ());//pop-up 12 where to return the call
</script>

<script>
Function Show (A, B)
{
return a+b;
}

Alert (Show (3, 5));//Popup 8
</script>

A function should return only one type of value
<script>
Function Show ()
{
}
Alert (Show ());
</script>
The popup undifined function can also have no return value

Function Pass Parameter

variable parameter (indeterminate parameter): Arguments is an array with variable number of arguments, array of arguments
Example summation

<script>
function sum ()
{
var result=0;
for (Var i=0;i<arguments.length;i++)
{
Result=result+arguments[i];
}
return result;
}
Alert (SUM (12, 6, 8, 6, 8,8));

CSS (odiv, ' width ') get style

Arguments[0]   Odiv
ARGUMENTS[1]   Width
ARGUMENTS[2]    200px


CSS (odiv, ' width ', ' 200px ') set the style

<div id= "Div1" style= "width:200px; height:200px; background:red; " > </div>

<script>

functioncss () {if(arguments.length==2)//Get    {        returnArguments[0].style[arguments[1]]; }    Else{arguments[0].style[arguments[1]]=arguments[2]; Set}}window.onload=function (){    varOdiv=document.getelementbyid (' Div1 '); //alert (CSS (odiv, ' width '));css (Odiv,' Background ', ' green ');};</script>

In the form of a parameter, obj, name, value

<script>functioncss (obj, name, value) {if(arguments.length==2)//Get    {        returnObj.style[name]; }    Else{Obj.style[name]=value; }}window.onload=function (){    varOdiv=document.getelementbyid (' Div1 '); Alert (CSS (Odiv,' Width ')); //css (odiv, ' background ', ' green ');//Settings};</script>

Take a non-inline style (cannot be used to set):
Obj.style used to get the inline style
OBJ.CURRENTSTYLE[ATTR] used to get non-inline style but only compatible with IE Firefox google incompatible
getComputedStyle (obj, false) [attr] compatible with Firefox google incompatible ie

Composite styles: Background, border
Single style: width, height, position

#div1 {width:200px; height:200px; background:red;}<script>functionGetStyle (obj, name) {if(obj.currentstyle) {returnObj.currentstyle[name]; }    Else    {        returngetComputedStyle (obj,false) [name]; }}window.onload=function (){    varOdiv=document.getelementbyid (' Div1 '); Alert (GetStyle (Odiv,' BackgroundColor ') ;//background, border belong to the compound style need to writeBackgroundColor, BorderWidth
};
<div id= "Div1" ></div>

Use of arrays

Two methods of array definition
var arr=[12, 5, 8, 9];
var arr=new Array (12, 5, 8, 9); There is no difference, [] The performance is slightly higher because the code is short

The property of the array length
Both can be obtained, and can be set
Example: quickly emptying an array

<script>
var arr=[1,2,3];
alert (arr.length); The length of the popup array
alert (arr.length=0);//sets the length of the array to 0, that is, to empty the array
</script>
Array usage principle: Only one type of variable should exist in the array
Array Add, delete element

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

Insert, delete
Splice (start, length) Delete
Splice (start, length, element ...) First Delete, then insert

Splice (start, length) Delete
<script>var arr=[1,2,3,4,5,6];arr.splice (// Delete: Splice (start, length) starting from the second position, starting with the number 3 , delete three alert (arr); // </script>
Splice (start, length, element ...) First Delete, then insert
<script>var arr=[1,2,3,4,5,6];arr.splice (// Delete First in join: Splice (start, length, element ...) Starting from the second position, starting with the number 3, delete 0 insertions 7,8,9alert (arr); // </script>




array Add, delete element Array Additions
, delete element
array, add, delete element















03. In-depth javascript

Related Article

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.