JavaScript value type and reference type instance description _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

<script type= "Text/javascript" >
var str = ' abced ';
var obj = new String (str);
function newtostring () {
Return ' Hello,world ';
}
function Func (val) {
val.tostring = newtostring;
}
Func (str); PS: actually only the value of STR is passed in, so the ToString modification to it is meaningless. The incoming STR is equivalent to one of his replicas. The method of modifying the replica does not affect the original result of the operation
Alert (str.tostring ()); The result is abced

Func (obj); PS: A reference to an object (that is, str itself, or a memory address) is passed in, so its toString modification will affect the subsequent alert (obj.tostring ()) Cloud Finder
Alert (obj.tostring ()); The result is Hello,world
</script>

Report:

Value types and reference types in JavaScript

Data type value/reference type note

Undefined value No value

Number value

Boolean value

String value strings are processed in assignment operations by reference type

function Primer

Object Primer
The value type and reference type of JS

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "> <title> basic Type/reference type-value type </title> <script text=" Text/javascript "> var a=1; var b=a; a=2; alert (b);//1 </script> </pead> <body> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

A variable of a value type directly holds a variable value, a reference to a variable, and a "key" that you can use to quickly find the memory area where the content is stored.
Value types are generally fixed byte sizes; Reference types tend to store arrays, objects, functions these implementations are difficult to know the amount of memory occupied.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "> <title> basic Type/reference type--reference type </title> <script text=" Text/javascript ">// References to the content occupy an unfixed size-like arrays, objects, functions var a=[1,2,3]; var b=a;//copy Reference value-imagine a pointer a[0]=100; alert (b);//100,2,3 </script> </pead> <body> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

A special string type between types in 2:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "> <title> basic Type/reference type--string </title> <script text=" Text/javascript ">//String More special, Length is not fixed should be referenced, but some behavior is like the basic type of var a= "Hello"; var b=a;//copy Reference Value--Imagine a pointer a= "Hello";//actually create another string "Hello" and point to IT Alert (b);//hello//String is a constant object </script> </HEAD&G T <body> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Each time a new string is created-----invariant object.
Add: Like the "Hello" string, when no variable references it (that is, the reference count is 0), it's time for garbage collection *-*

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.