怎样在JS函数中传递参数?
怎样在JS函数中传递参数?
例如我有3个function:
function f1(name),function f2(name),function f3(name)
funtion f1(name){
f2(name);
}
funtion f2(name){
f3(name);
}
在function f3(name)中的参数name得到的不是f1中的参数name的值?
要怎样传,name是字符串时怎么传,name为object时又该怎样?
------解决方案--------------------1.全局变量
2.函数形参
JS没有引用,所以怎么传不是我们所能控制的~~
------解决方案--------------------在js中,值传递有两种,一种是值传递,一种是引用传递
string,number,boolean这三种是按值传递的
array,date等是按引用传递的
<script type= "text/javascript ">
<!--
function f1(name){ return name;};
function f2(name){ return f1(name);};
function f3(name){ return f2(name);};
function f4(name){ if(typeof name == "string ") name = "changed at f4 "; else name.setYear(3000);};
var str = "this is a string. ";
var ar = new Date();
document.write(f3(str), " <br> ");
document.write(f3(ar), " <br> ");
f4(str);
f4(ar);
document.write(str, " <br> ");
document.write(ar, " <br> ");
//-->
</script>