Summary on the differences between byval and byref

Source: Internet
Author: User

Differences:
Byval is used to pass a value. The real parameter and the form parameter have different memory units, so they do not interfere with each other!
Byref transfer address. The real parameter and the form parameter occupy the same memory unit. If the form parameter is changed, the real parameter is changed !!!!!!
Easy to understand:
Byval is gone forever
Byref may be updated!
In javascript:
Boolean, number, and string parameters are passed by value ==> equivalent to byval in vbs;
Object parameters (including JS objects, array objects, and function objects) are passed by reference ==> equivalent to byref in vbs. CopyCode The Code is as follows: <! 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> function value passing test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta name = "author" content = "fengyan, 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 type parameter, passing by value => is equivalent to byval in vbs;
Function OBJ (){}
OBJ. Prototype. Show = function (o) {// JS object, which is 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 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>
</Html>

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.