JS関数パラメータ伝達


Js関数パラメータを使用して伝達する場合は、他のプログラミング言語と同様に、値伝達か参照伝達かに注意します。
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Tests</title>
<script language="javascript" type="text/javascript">
function alterArgs(strLiteral, argObject){
strLiteral = "override";
argObject[0] = 2;
argObject[1] = 3;
}

function testParams(){
var str = "origal";
var arg = new Array("two", "three");

document.writeln(str + "<br/>");
document.writeln(arg + "<br/>");
alterArgs(str, arg);
document.writeln(str + "<br/>");
document.writeln(arg + "<br/>");

}
</script>
</head>
<body onload = "testParams();">  
</body>
</html>
 
//    :
//origal
//two,three
//origal
//2,3
    基本的なデータの種類として、文字列は、値を伝えるものとして見られます。対象のArayについては引用伝達を行う。
    次にもう一つの比較を見に来ます。
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Tests</title>
<script language="javascript" type="text/javascript">
function alterArgs(strLiteral, argObject){
strLiteral = new Date();
argObject[0] = 2;
argObject[1] = 3;
}

function testParams(){
var str = new Date(55555555555);
var arg = new Array("two", "three");

document.writeln(str + "<br/>");
document.writeln(arg + "<br/>");
alterArgs(str, arg);
document.writeln(str + "<br/>");
document.writeln(arg + "<br/>");

}
</script>
</head>
<body onload = "testParams();">  
</body>
</html>
//    :
//Wed Oct 06 1971 08:05:55 GMT+0800 (China Standard Time)
//two,three
//Wed Oct 06 1971 08:05:55 GMT+0800 (China Standard Time)
//2,3