Javascript-can the data in the stack be modified? For example, can the value data be modified?

Source: Internet
Author: User
The {code...} string is immutable, so it cannot be modified. can the data in the stack be modified? For example, num10; num11; change the value 10 in the stack to 11, or create a new num11 in the stack to delete the original memory?
Var str = "abc"; str [0]; // "a" str [0] = "d"; str; // still "abc"

The string is immutable, so it cannot be modified.

Can the data in the stack be modified?
For example, num = 10; num = 11;
The value of 10 in the stack is changed to 11,
Or create a new num = 11 in the stack to delete the original memory?

Reply content:
Var str = "abc"; str [0]; // "a" str [0] = "d"; str; // still "abc"

The string is immutable, so it cannot be modified.

Can the data in the stack be modified?
For example, num = 10; num = 11;
The value of 10 in the stack is changed to 11,
Or create a new num = 11 in the stack to delete the original memory?

In my understanding

Var str = "abc"; // create a string type of the original value in js, and the original value is an immutable str [0] = "d "; // This sentence is a typical syntax for referencing object attributes. we can understand it as follows: str is actually immutable, but writing like this will cause strTemp = new String (str ); created, that is, temporary object creation, and assigned strTemp [0] = "d";, and then destroyed immediately. That is, you must remember that the original value of js is immutable. Objects are variable.
Let's put it this way. I use a string for example: var str = "Hello"; // "Hello" is a literal, the original value (Primitive Type, there is a memory storage "Hello", str is a variable name, used to reference this memory, use it. Var str = "World"; // This sentence actually changes the position of the reference, because the original value remains unchanged. then, if the above "Hello ", if this is not necessary, automatic garbage collection of js will be performed.

If I still don't understand it, I will enlarge the secret of the original js value.

Make progress together.

The string of the Script is immutable ).

  1. The pointer [0] in str [0] = "d"; can read its value.

Supplement:

Methods defined by the String class cannot change the content of the String. A method like String. toUpperCase () returns a brand new String instead of modifying the original String.

Suggestion:

Refer to the above information.

Come on!

It should be newly created.

Memory distribution, heap, stack, and constant pool. The heap space is larger than the stack space. objects and functions are usually stored in the heap. The declared variables are put in the stack. the constant pool usually contains some common characters and numbers. So I added that some items are stored in different locations.

The string type in javascript is referenced in the string type in Java. Once the string literal is declared, it cannot be changed.

There is a hermit conversion in it

Variable (value) → memory
In values and Boolean variables, (value) is a value.
In strings, objects, arrays, etc., the (value) is the address pointing to the memory. the comparison is (value ).
When you assign a value to a variable again, you can change the value. if you assign a value to another variable, you can [Copy] the value to another variable.

Figure
N = 10; n = 11; n remains unchanged. The value is changed ).
B = n; value transfer, n unchanged, B = 10.
Str = "aa"; str = "bb"; str unchanged, (value) changed to another string.
Str2 = "aa", str2 is different from str, pointing to not the original "aa" but the new "aa ".
Str3 = str2; the value of str3 is the same as that of str2, pointing to the same string, equivalent.

Strings are copied and transmitted by reference and compared by value.
Values of the Numbers and Boolean types (true and false) are copied, transmitted, and compared by value.
Objects, arrays, and functions are copied, transmitted, and compared by reference.

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.