tutorial on JavaScript function parameters and scopes

Source: Internet
Author: User
Tags variable scope


Function argument Small example one:

FnA (1,2);
FnA (2,3);
function FnA (a,b)
{
alert (A+B);
}

Function argument Small instance one:

Css:
body,ul{margin:0;padding:0;}
li{List-style:none;}
. box{width:200px; margin:20px Auto;
. box ul{height:30px;
Box li{width:50px;height:30px;background: #ccc; color: #000; float:left; text-align:center;line-height:30px; font-size:20px;}
#box1. Active1{background: #333; color: #fff;}
#box2. Active2{background: #666; color:red;}
. content{height:60px;border:1px solid #ccc;}
. Content div{display:none;font-size:50px;
. content. show{Display:block;
Js:
Window.onload=function ()
{
tab ("Box1", "active1");
tab ("Box2", "active2");
};
Function tab (SID,SCLASSNAME)
{
var Obox=document.getelementbyid (sId);
var ali=obox.getelementsbytagname ("ul") [0].getelementsbytagname ("Li");
var adiv=obox.getelementsbytagname ("div") [0].getelementsbytagname ("div");
for (Var i=0;i<ali.length;i++)
{
Ali[i].index=i;
Ali[i].onmouseover=function ()
{
for (Var i=0;i<ali.length;i++)
{
Ali[i].classname= "";
Adiv[i].classname= "";
}
This.classname=sclassname;
Adiv[this.index].classname= "Show";
};
}
}
Html:
<div class= "box" id= "Box1" >
<ul>
<li class= "Active1" >1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div class= "Content" >
<div class= "Show" >1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</div>
<div class= "box" id= "Box2" >
<ul>
<li class= "Active2" >1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div class= "Content" >
<div class= "Show" >1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</div>
Scope Small Example One:
function Fnaa ()
{
var b=0;
}
function FnBB ()
{
Alert (b)
}
FnBB (); The result is: b is undefined;

A variable defined within a function is called a local variable scope and is only valid within the function
Scope Small Example Two:
var b=0;
function FnBB ()
{
B-=1;
}
FnBB (); -1
FnBB (); -2
alert (b); So-2

Globally defined variables, called global variables, are global in scope and can be modified and used in the global context.
Scope Small Example Three:
function Fnaa ()
{
var a=0;
function FnBB ()
{
var b=0;
alert (a);
}
FnBB ();
}
Fnaa ();

A variable defined within a function is called a local variable scope and is only valid within the function

Globally defined variables, called global variables, are global in scope and can be modified and used in the global context.

function is a scoping problem

The closed Bun function can use the parent function's local variables but the parent function cannot access the contents of the child elements inside

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.