How JavaScript and C # handle variables

Source: Internet
Author: User

Let's take a look at JavaScript (the basic type and the simple type below are a meaning):

There are two types of variables in JavaScript, one of which is the basic type, a total of five, with null, Bollean, undefined, number, and string. A primitive type is a simple piece of data that is stored directly in a variable, that is, the access variable is a direct access to the value in the variable and is accessed by value. Another is a reference type, where the value of the reference type is stored in memory (heap), and if the value of the reference type is to be accessed, it is accessed through a variable that points to the reference type of the object. JavaScript does not allow direct manipulation of the object's memory space. It is worth noting that the string type in JS is a value type. Whether it is a value type or a reference type, the variable in JS is simply the name of the saved value, which can be used to hold any type of value. After defining a variable, this variable can save any other type of value, anywhere. The difference between a variable of a simple type and a variable of a reference type is that the mechanism for copying is different. Copying a variable of a primitive type duplicates the value of the original variable, which means that the two are completely independent after the copy, and the operation on one does not cause another change. When copying a variable of a reference type, it is also a copy of the value in the variable, but the difference is that the value contained in the variable of the reference type is a pointer to the real object in memory (heap). This means that after copying the two variables are shared with one object, and any one of them will change the object after the operation:

var obj1 = new Object (); var obj2 = obj1; Obj1. Name = "Nicholas"; Alert (obj2. Name); "Nicholas"

The pass parameter aspect JS is to pass by the value.

The parameters of all functions in ECMAScript are passed by value. In other words, copying the values from the outside of the function to the parameters inside the function is the same as copying the value from one variable to another. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference to a value of a type is the same as a copy of a reference type variable. There are a lot of developers at this point who may
Confused because the access variable has both by value and by reference, and parameters can only be passed by value. Zecas (Zakas. Nicholas C.). JavaScript Advanced Programming (3rd Edition) (Turing Programming series) (Kindle location 2623-2626). People's post and telecommunications press. Kindle version.

  

 

In the case of C #: C # also contains two data types, one simple data type, byte, short, int, long, Flaot, double, Boolean, Char, and so on, all simple data types, also called value types, Its value is stored in the variable itself, the operation of this type of variable will directly manipulate the variable itself, and JS is different from the string is the object type, the value is stored on the heap. Another is the object type, also called the reference type, the object is stored on the heap after the instantiation of the name, when the new object type, will return a reference to the object, the reference (value) stored in a variable on a stack (about the instantiation value type variable after the specific storage location, The timing and locality of the instantiation of a value type variable. is described in C # in depth. ), that is, the value of the variable holds a reference, and the object is manipulated by this reference to manipulate the objects stored on the heap. This also causes the fundamental difference between the value type and the reference type in C # in that the mechanism of replication is different. In C # Whether a value type or a reference type can be passed by value and by reference, passing by value is a copy of the value of the variable, and passing by reference is actually passing an "alias" of an original variable, that is, passing the value of the action is another piece of data, Passing the word by reference is the same data as the operation, but the consequences of this in the value type and reference type are slightly different, because the value type is stored in the variable, and if it is passed by value, it is copied together with the variable and value. This means that there is another space on the stack to hold the copy, so it will not have any effect on the original value. Look at the following example:

Class program    {        static void Main (string[] args)        {            int shit = 2;            Console.WriteLine ("Before:" +shit);//Return 2            dosome (shit);            Console.WriteLine ("After:" +shit);//returns 2            Console.WriteLine (dosome (Shit));//returns 3            Console.readkey ();        }        public static int dosome (int shit)        {            shit++;            return shit;        }    }

If you pass a variable of a value type by reference, you pass the original variable as an alias, and in fact the "Alias" and the original variable share a value. Then whether the "alias" operation or the original variable will change the value, see the following example:

Class program    {        static void Main (string[] args)        {            int shit = 2;            Console.WriteLine ("Before:" +shit);//returns 2            dosome (ref shit);            Console.WriteLine ("After:" +shit);//returns 3            Console.WriteLine (Dosome (ref shit));//returns 4            console.readkey ();        }        public static int dosome (ref int shit)        {            shit++;            return shit;        }    }

All of this is the difference between the value-based and by-reference delivery of value types in C #. By the way, in C #, passing by reference is divided into ref and out, and these two keywords are used to denote passing by reference, that is, the timing of the initialization of an argument is different. One is that it must be initialized before use (out), and ref has no such requirement.

For reference types in C #, pass-by-value and by-reference, and the performance of value-type-by-value and by-reference are different, the value-passing of a reference type is also a copy of the value of a reference type, unlike a value type that is a reference to an object, which is an address that is saved. So it is also possible to change variables on the heap when a variable of a reference type is passed by value, or it may not change, such as passing a variable of a reference type to its value, and then doing a new operation on it immediately, so that it points back to the newer address on the heap. : While passing a reference to a variable of a reference type is also an alias, the alias and the original object share a value (a reference to the object on the heap) so any operation on the alias will change the object on the heap.

18:21:51

How JavaScript and C # handle variables

Related Article

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.