网络编程
位置:首页>> 网络编程>> JavaScript>> js关于 byval 与 byref 的区别

js关于 byval 与 byref 的区别

作者:CNLei 来源:CNLei.blog 发布时间:2007-10-13 10:48:00 

标签:byval,byref,参数,js

js关于 byval 与 byref 二者区别:

byval 传递数值,实参和形参分处不同的内存单元,互不干扰!
byref 传递地址,实参和形参占用相同的内存单元,形参变则实参变!!!!!!

通俗理解:

byval 一去不复返
byref 进去再出来,可能被更新!

在JavaScript中:
Boolean,Number,String型的参数是按值传递的 ==> 相当于VBS中的ByVal;
而Object型的参数(包括JS对象,Array对象,Function对象等),是按引用传递 ==> 相当于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="cnlei.y.l@gmail.com , 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+"\n"+O.toString());
F.show(O);
alert(O.id+"\n"+O.toString());
Func(F);
F.show(O);
alert(O.id+"\n"+O.toString());
//-->
</script>
</body>
</html>

在JavaScript中:

Boolean,Number,String型的参数是按值传递的 ==> 相当于VBS中的ByVal;
而Object型的参数(包括JS对象,Array对象,Function对象等),是按引用传递 ==> 相当于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="cnlei.y.l@gmail.com , 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+"\n"+O.toString());
F.show(O);
alert(O.id+"\n"+O.toString());
Func(F);
F.show(O);
alert(O.id+"\n"+O.toString());
//-->
</script>
</body>
</html>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com