A summary of the difference between ByVal and ByRef _javascript Skills

Source: Internet
Author: User
Tags numeric value
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"/>
<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;
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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.