-------------------------------------------------
一个函数就是一段封装的代码,这段代码与其他代码(函数外的代码)进行的数据传递是通过函数的参数来实现的。
函数的参数分两种,一种是形式参数,一种是实际参数。
function sum(a,b){//这里函数定义的时候的参数就是形式参数
return a+b;
}
var result = sum(1,2)//当调用函数时候,此时传递进去的真实的数据为实际参数
------------------------------------------------
function sum(){//无参数的函数必须在其函数名后加括号
var temp= 0;
var realArgumentCount=arguments.length;//这里得到的长度是实际参数长度
var frmArgumentCount=sum.length;//表示形式参数的长度
for(i=0;i<realArgumentCount;i++){//arguments对象可以用来检测参数个数
temp+=arguments[i];
}
return res;
}
sum(1,2,3,4);//传入1234求和为10
------------------------------------------------------
利用内置对象arguments判断传递给函数的实际参数个数,即可模拟函数重载
function convertOverloading() {
if(arguments.length == 1)
{
document.write(arguments[0]);
}
else if(arguments.length == 2)
{
document.write(arguments[0] + arguments[1]);
}
}
convertOverloading(10); //输出 "10"
convertOverloading(40, 20); //函数名相同参数不同输出 "60"
Javascript在创建函数的同时,会在函数内部创建一个Arguments对象实例arguments,arguments是函数参数的数组,将它称为数组其实有些牵强,因为更确切地说它是一个Arguments对象实例,但是它确实能够像数组一样用"[]"引用它的内部元素。
----------------------------------------------------------
1.arguments: A JavaScript Oddity
arguments is the name of a local, array-like object available inside every function. It’s quirky, often ignored, but the source of much programming wizardry; all the major JavaScript libraries tap into the power of the arguments object. It’s something every JavaScript programmer should become familiar with.
2.Convert it to a Real Array
Even though arguments is not an actual JavaScript array we can easily convert it to one by using the standard Array method, slice, like this:
var args = Array.prototype.slice.call(arguments);
The variable args will now contain a proper JavaScript Array object containing all the values from the arguments object.
(http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/)
一个函数就是一段封装的代码,这段代码与其他代码(函数外的代码)进行的数据传递是通过函数的参数来实现的。
函数的参数分两种,一种是形式参数,一种是实际参数。
function sum(a,b){//这里函数定义的时候的参数就是形式参数
return a+b;
}
var result = sum(1,2)//当调用函数时候,此时传递进去的真实的数据为实际参数
------------------------------------------------
function sum(){//无参数的函数必须在其函数名后加括号
var temp= 0;
var realArgumentCount=arguments.length;//这里得到的长度是实际参数长度
var frmArgumentCount=sum.length;//表示形式参数的长度
for(i=0;i<realArgumentCount;i++){//arguments对象可以用来检测参数个数
temp+=arguments[i];
}
return res;
}
sum(1,2,3,4);//传入1234求和为10
------------------------------------------------------
利用内置对象arguments判断传递给函数的实际参数个数,即可模拟函数重载
function convertOverloading() {
if(arguments.length == 1)
{
document.write(arguments[0]);
}
else if(arguments.length == 2)
{
document.write(arguments[0] + arguments[1]);
}
}
convertOverloading(10); //输出 "10"
convertOverloading(40, 20); //函数名相同参数不同输出 "60"
Javascript在创建函数的同时,会在函数内部创建一个Arguments对象实例arguments,arguments是函数参数的数组,将它称为数组其实有些牵强,因为更确切地说它是一个Arguments对象实例,但是它确实能够像数组一样用"[]"引用它的内部元素。
----------------------------------------------------------
1.arguments: A JavaScript Oddity
arguments is the name of a local, array-like object available inside every function. It’s quirky, often ignored, but the source of much programming wizardry; all the major JavaScript libraries tap into the power of the arguments object. It’s something every JavaScript programmer should become familiar with.
2.Convert it to a Real Array
Even though arguments is not an actual JavaScript array we can easily convert it to one by using the standard Array method, slice, like this:
var args = Array.prototype.slice.call(arguments);
The variable args will now contain a proper JavaScript Array object containing all the values from the arguments object.
(http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/)