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 *-*