js关于 byval 与 byref 的区别

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

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>

标签:byval,byref,参数,js
0
投稿

猜你喜欢

  • z-index在IE中的迷惑

    2007-05-11 16:50:00
  • CSS实例讲解:地图提示

    2007-05-11 17:04:00
  • 未能找到存储过程’master.dbo.xp_fileexist’解决方法

    2011-03-13 09:29:00
  • DBA_2PC_PENDING 介绍

    2009-02-28 10:59:00
  • asp 关键词高亮显示(不区分大小写)

    2011-04-07 10:55:00
  • javascript 获取中文字符串长度

    2009-10-18 12:06:00
  • IE中选择符的4095限制

    2009-10-09 13:25:00
  • MySQL数据库中CHAR与VARCHAR之争

    2011-05-05 16:33:00
  • 在系统崩溃的时候如何恢复原有的数据

    2009-01-08 13:26:00
  • SQL server使用自定义函数以及游标

    2011-11-03 17:26:27
  • SQL 统计一个数据库中所有表记录的数量

    2012-01-29 18:21:36
  • Mootools 1.2教程(23)——滑动效果(Slide)

    2009-03-04 12:33:00
  • 详尽解析javascript的event对象

    2008-01-16 11:27:00
  • VMware中Linux共享mysql数据库

    2010-10-25 20:29:00
  • Overflow Auto && Position Relative

    2009-09-03 12:02:00
  • 关于分页查询和性能问题

    2008-03-11 12:25:00
  • javascript面向对象技术基础(二)

    2010-02-07 13:09:00
  • 初衷和结果

    2009-02-23 12:52:00
  • 实例剖析:MySQL数据库优化详解

    2008-11-22 12:19:00
  • 如何使用ADO.NET连接数据库?

    2010-06-03 10:52:00
  • asp之家 网络编程 m.aspxhome.com