undefinedfix
Sign in

After writing such a piece of code, the execution result is not the same as expected. Why?

pjson edited in Sat, 19 Nov 2022

The code is as follows:

const testFunc = (str)=>{
    str = 'my test str'
}
let str = ''
testFunc(str)
console.log(str)

January 15, 2020 23:18:16 PM new Canada:

I'm confused for a moment, because I often operate the object directly in the function. When I change it to a string, I find that it doesn't work and I don't react. The code is as follows:

let a = { name: '11111', sex: 'nan' }
const setName = (param) => {
    console.log(param === a)
    param.name = 'test'
}
setName(a);
console.log(a)

If the above setname (a) is replaced by setname (a.name), the param.name =Replace 'test' with param ='test ', and you can see that the result is different again.

The reason for the above results is that the global scope and function scope are not fully understood

  • When the input parameter is a constant, the change of the parameter is in the function scope and will not affect the value of the global scope.
  • When the passed in parameter is an object, although the external function is a global scope and the parameter is a function scope, the value in the global scope and the value in the function scope will change when the content changes because the object uses the same memory.
5 Replies
user618075
commented on Sat, 19 Nov 2022

In ECMAScript, the parameters of all functions are passed by value, and the function parameters are not the references of variables, but the copies of variables. When a function parameter is an object (that is, a reference type is not a simple value, but an address in the heap memory that points to the object), the function operates on the object's properties internally, which is actually the same as the value in the heap memory that the external variable points to. For example, let a = {A: 1, B: 2}; let B = a, B.A = 3, then A.A is equal to 3. If you are interested, you can search stack memory and heap memory

iZucken
commented on Sun, 20 Nov 2022

One thing to be clarified is what is "variable value" when variables store reference types and basic types

Consider this Code:

const anyIns = { a: 1 }
const fn = any => { // 新建变量 any,存储了引用类型的指针
  console.log(any === anyIns) // 两个指针一样,所以是 true
  any.a = 2 // 指针对应的内存修改
}
console.log(anyIns)
fn(anyIns)
console.log(anyIns)

So the result is

{ a: 1 }
true
{ a: 2 }

For basic types:

const anyIns = 1
const fn = any => { // 新建变量 any,存储了基本类型的值
  console.log(any === anyIns) // 值相等
  any = 2 // 变量的值进行了修改
}
console.log(anyIns)
fn(anyIns)
console.log(anyIns)

So the result is

1
true
1
schillerlawoffices
commented on Sun, 20 Nov 2022

JS pit, one of the complex type, basic type. Address and value

comptech08
commented on Sun, 20 Nov 2022

It is not a problem of global scope or function scope, but a problem of reference data type and basic data type.

When a reference data type is passed into a function, the reference is passed in, not the value itself.

A reference can be simply understood as an address to a memory space, which is also a string, as shown in the following figure.

image.png

Therefore, when you pass a reference into a function and manipulate the reference in the function, the last thing to be modified is the value itself.

On the scope, data type, more detailed answers, welcome to subscribe to my books to learn

https://xiaozhuanlan.com/afed

Titirca
commented on Sun, 20 Nov 2022

The print is still let defined '', which has nothing to do with the arrow function.