Home > Backend Development > PHP Tutorial > javascript - Can the data in the stack be modified? For example, can numerical data be modified?

javascript - Can the data in the stack be modified? For example, can numerical data be modified?

WBOY
Release: 2016-08-23 09:17:49
Original
1424 people have browsed it

<code>var str = "abc";
str[0]; // "a"
str[0] = "d";
str; //仍然是"abc"</code>
Copy after login
Copy after login

The string is immutable so it cannot be modified

Then can the data in the stack be modified?
For example, num=10;num=11;
What is changed is the value 10 in the stack is changed to 11,
or a new num=11 is created in the stack and the original memory is What about deletion?

Reply content:

<code>var str = "abc";
str[0]; // "a"
str[0] = "d";
str; //仍然是"abc"</code>
Copy after login
Copy after login

The string is immutable so it cannot be modified

Then can the data in the stack be modified?
For example, num=10;num=11;
What is changed is the value 10 in the stack is changed to 11,
or a new num=11 is created in the stack and the original memory is What about deletion?

My understanding is

<code>var str = "abc";//创建了一个js中原始值string类型,而原始值是不可变的
str[0] = "d";//这句是典型的引用对象属性的语法,我们可以这样理解,str实际上是不可变的,但是你这样
书写,会造成  strTemp = new String(str);被创建,也就是临时对象创建,并且赋值strTemp[0] = "d";
之后立即销毁。
也就是你要记住一点,js中原始值是不可变的。
对象是可变的。</code>
Copy after login
<code>这么说吧,我用字符串举例:
var str = "Hello";//"Hello"是一个字面量,原始值(Primitive Type),我是这样理解的,有一块内存存储"Hello",str是一个变量名而已,用来引用这块内存,使用它。

var str = "World";//这一句实际上只是改变了引用的位置,因为原始值是不变的,然后假如上面的"Hello",不用的话js的自动垃圾处理回回收的。</code>
Copy after login

If you still don’t understand, I will try my best------>The secret of js original value

Make progress together.

Script strings are immutable.

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

Added:

<code>String 类定义的方法都不能改变字符串的内容。像 String.toUpperCase() 这样的方法,返回的是全新的字符串,而不是修改原始字符串。</code>
Copy after login

Recommendation:

<code>以上信息来 http://www.w3school.com.cn/jsref/jsref_obj_string.asp
遇到你感觉比较基础的问题,多查文档。
</code>
Copy after login

Come on!

should be newly created
javascript - Can the data in the stack be modified? For example, can numerical data be modified?

Memory distribution, including heap, stack, and constant pool. The space of the heap is larger than that of the stack. The heap usually contains objects, functions and the like. The declared variables are placed in the stack, and the constant pool generally contains regular characters and numbers. So what I would like to add is that some things are stored in different locations than you said.

The string type in JavaScript is borrowed from the string type in Java. Once the string literal is declared, it cannot be changed

There is a hermit conversion inside

javascript - Can the data in the stack be modified? For example, can numerical data be modified?

Variable (value)→Memory
In numerical and Boolean variables, (value) is a numerical value
In strings, objects, arrays, etc., (value) is an address pointing to memory. What is compared is (value).
When a variable is reassigned, it is changing the (value), and assigning a variable to another variable is passing (copying) the (value) to another variable.

In the picture,
n=10; n=11; n remains unchanged, what changes is (value).
b=n; value is passed, n remains unchanged, b=10.
str="aa";str="bb "; str remains unchanged, (value) changes to point to another string.
str2="aa", str2 is different from str, it does not point to the original "aa" but the new "aa".
str3=str2;str3 The (value) is the same as str2, pointing to the same string, equivalent.

Strings are copied and transferred by reference and compared by value
Numbers and Boolean type values ​​(true and false) are copied, transferred and compared by value
Objects, arrays and functions are copied and transferred by reference And compare

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template