The difference between the two:
ByVal Pass The numeric value, the actual parameter and the parameter branch place different memory unit, do not disturb each other!
ByRef passes the address, the argument and the formal parameter occupy the same memory unit, and the parameter variable is the actual parameter variable!!!!!!
Popular Understanding:
ByVal is Gone forever.
ByRef goes in and comes out again, may be updated!
In javascript:
The parameter of the boolean,number,string type is passed by the value ==> equivalent to the ByVal in the VBS;
The parameters of object type (including JS object, Array object, function object, etc.) are passed by reference ==> equivalent to ByRef in a VBS
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> function Pass-value test </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "Maple rock, Cnlei"/>
<meta name= "Copyright" content= "cnlei.y.l@gmail.com, http://www.cnlei.com"/>
<body>
<script type= "Text/javascript" >
<!--
function Num (n) {n=n*2;} The parameter of number type, the ==> that is passed by value is equivalent to the ByVal in the VBS;
function Obj () {}
Obj.prototype.show = function (o) {//js object, passed by reference ==> equivalent to ByRef in VBS
o.tostring = function () {
Return ("{ID:" +this.id+ ", desc:" +this.desc+ "});
}
}
function Func (f) {//function object, which is passed by reference ==> equivalent to ByRef in a VBS
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: "Number description",
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>