gpt4 book ai didi

javascript - 如何正确传递 Javascript 属性以便它们可以被函数修改

转载 作者:行者123 更新时间:2023-12-04 17:11:11 25 4
gpt4 key购买 nike

我知道 JavaScript 对象(包括数组)是通过引用自动传递的,所以我有这样的东西:

class Thing {
constructor( ) {
this.letters = [ "A", "B" ];
this.numbers = [ 1, 2, 3 ];

this.change( this.letters, this.numbers );
}

change( _letters, _numbers ) {
let l = _letters;
let n = _numbers;

l = [ ];
n = 6;
}
}

var t = new Thing( );
console.log( t );

日志请求显示 Thing 的属性没有变化。然而,在我无法识别的不同条件下,我不小心修改了函数内传递的参数,并且不得不使用类似 n = Array.from( _numbers ) 的东西来避免修改源属性. 有人能解释发生了什么,为什么?

我想通过引用传递对象属性,这样我就可以在单个函数调用中修改多个相关属性,从而保留返回值以供调试。

谢谢。

最佳答案

问题是范围界定。当您使用 let 声明时,该声明的作用域为函数 change 并且您还在此处重新分配 block 作用域变量:

l = [ ];
n = 6;

如果你想改变类的属性,你可以这样做:

change(_letters, _numbers) {
this.letters = _letters; // `this` scoped to class
this.numbers = _numbers; // `this` scoped to class
}

看例子:

// simple demo start
let numbers = [1, 2, 3];

let n = numbers;
n.push(4)
console.info(n, typeof n);

n = 6; // reassign n
console.info(n, typeof n, numbers);
// simple demo end

// refactored class
class Thing {
constructor() {
this.letters = ["A", "B"];
this.numbers = [1, 2, 3];

this.change(this.letters, this.numbers);
}

change(_letters, _numbers) {
let l = _letters; // `let` is scoped to `change()`
let n = _numbers; // `let` is scoped to `change()`

l = []; // only changes scoped l
n = 6; // only changes scoped n
}

changeFix(_letters, _numbers) {
this.letters = _letters; // `this` scoped to class
this.numbers = _numbers; // `this` scoped to class
}
}

var t = new Thing();
console.log(t);

t.change(['C'], [4]);
console.info(t);

t.changeFix(['C'], [4]);
console.info(t);

关于javascript - 如何正确传递 Javascript 属性以便它们可以被函数修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69410948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com