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
投稿

猜你喜欢

  • 详解Python函数print用法

    2023-06-10 03:47:34
  • 简单了解Python读取大文件代码实例

    2022-10-23 11:46:19
  • Goland 2020或2019软件版本去掉a...或fmt...提示的方法

    2024-04-25 15:06:44
  • Python调用系统命令os.system()和os.popen()的实现

    2021-06-27 23:56:29
  • python序列类型种类详解

    2022-02-27 13:53:38
  • JavaWeb实现显示mysql数据库数据

    2024-01-20 09:39:43
  • python查看微信好友是否删除自己

    2021-01-31 23:16:32
  • mssql中获取指定日期所在月份的第一天的代码

    2011-09-30 11:23:57
  • python转换字符串为摩尔斯电码的方法

    2022-01-01 10:17:44
  • Pandas缺失值填充 df.fillna()的实现

    2023-11-24 00:01:41
  • python生成requirements.txt文件的推荐方法

    2021-02-04 01:02:30
  • 详解Selenium+PhantomJS+python简单实现爬虫的功能

    2023-03-09 01:09:00
  • python3利用Socket实现通信的方法示例

    2022-04-10 03:09:04
  • MySQL中登录与退出超全图文讲解

    2024-01-19 20:24:43
  • SQL Server 常用函数使用方法小结

    2024-01-13 11:39:32
  • Python 取numpy数组的某几行某几列方法

    2023-11-24 05:46:47
  • 一个输入框提示列表效果

    2008-03-09 18:53:00
  • Python中的pprint模块

    2022-02-07 17:02:42
  • django 通过ajax完成邮箱用户注册、激活账号的方法

    2022-02-19 05:59:57
  • JavaScript中filter的用法实例分析

    2024-04-10 10:59:42
  • asp之家 网络编程 m.aspxhome.com