byvalとbyrefの違いについての分析まとめ
両者の違い:byval 転送値は、実際の参照とイメージの異なるメモリユニット、相互干渉しません! byref 転送アドレスは、実参照と形参が同じメモリユニットを占有しています。分かりやすい:byval 行ったきり帰らない byref 中に入ってまた出ます。更新されるかもしれません。JavaScriptでは、Boolean、Number、Stringタイプのパラメータは値で伝達されます。 ==> VBSの中のByValに相当します。Object型のパラメータ(JSオブジェクト、Arayオブジェクト、Functオブジェクトなどを含む)は、参照によって伝達されます。 ==> VBSに相当するByRef
<!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" lang="zh-CN">
<head>
<title> </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content=" ,CNLEI" />
<meta name="copyright" content="[email protected] , http://www.cnlei.com" />
</head>
<body>
<script type="text/javascript">
<!--
function Num(n){n=n*2;}//Number , ==> VBS ByVal;
function Obj(){}
Obj.prototype.show = function(o){ //JS , ==> VBS ByRef
o.toString = function(){
return("{id:"+this.id+",desc:"+this.desc+"}");
}
}
function Func(f){ //Function , ==> VBS ByRef
f.show = function(o){
o.toString = function(){
return("{id:"+this.id+",desc:"+this.desc+",toString:function(){} }");
}
}
}
var N;
N=1;
alert(N);
Num(N);
alert(N);
var O;
O = {
id:"001",
desc:" ",
toString: function (){
return null;
}
};
var F = new Obj();
var F2 = new Obj();
alert(O.id+"
"+O.toString());
F.show(O);
alert(O.id+"
"+O.toString());
Func(F);
F.show(O);
alert(O.id+"
"+O.toString());
//-->
</script>
</body>
</html>