Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中使用现有变量?_Javascript - Fatal编程技术网

如何在JavaScript中使用现有变量?

如何在JavaScript中使用现有变量?,javascript,Javascript,看起来第4行正在创建一个新变量。如何告诉JavaScript使用在第2行中创建的现有变量x?x不是指向列表中第一个元素的指针。因此,当它被更改时,它不会影响列表中的第一个元素。如果希望更改,则需要明确执行以下操作: var list = [{name: 'foo'}, {name: 'bar'}]; var x = list[0]; x.name = 'baz'; // Changes name in list[0] x = {name: 'baz'} // Doesn't change nam

看起来第4行正在创建一个新变量。如何告诉JavaScript使用在第2行中创建的现有变量x?

x不是指向列表中第一个元素的指针。因此,当它被更改时,它不会影响列表中的第一个元素。如果希望更改,则需要明确执行以下操作:

var list = [{name: 'foo'}, {name: 'bar'}];
var x = list[0];
x.name = 'baz'; // Changes name in list[0]
x = {name: 'baz'} // Doesn't change name in list[0] - PROBLEM

x不是指向列表中第一个元素的指针。因此,当它被更改时,它不会影响列表中的第一个元素。如果希望更改,则需要明确执行以下操作:

var list = [{name: 'foo'}, {name: 'bar'}];
var x = list[0];
x.name = 'baz'; // Changes name in list[0]
x = {name: 'baz'} // Doesn't change name in list[0] - PROBLEM

变量
x
保存对对象的引用

  • x.name=…
    更改
    x
    引用的特定对象的属性

  • x={…}
    更改
    x
    引用的对象

以下是设置:

// you are creating an array of two objects
var list = [{name: 'foo'}, {name: 'bar'}];
//assigning the first obj to a variable
var x = list[0];
//changing the property 'name' of the object held in variable x
x.name = 'baz'; //--Changes name in list[0]--(no it doesn't!!!, it only changes the property of the object held in variable x!, not the one in the array)
//now you are assigning a new object to that variable!!!
x = {name: 'baz'}
但您希望它这样做(注意:这种情况永远不会发生):

x ==> { brand new object } 
      {    old object    } <== list[0]

x==>{brand new object}变量
x
包含对对象的引用

  • x.name=…
    更改
    x
    引用的特定对象的属性

  • x={…}
    更改
    x
    引用的对象

以下是设置:

// you are creating an array of two objects
var list = [{name: 'foo'}, {name: 'bar'}];
//assigning the first obj to a variable
var x = list[0];
//changing the property 'name' of the object held in variable x
x.name = 'baz'; //--Changes name in list[0]--(no it doesn't!!!, it only changes the property of the object held in variable x!, not the one in the array)
//now you are assigning a new object to that variable!!!
x = {name: 'baz'}
但您希望它这样做(注意:这种情况永远不会发生):

x ==> { brand new object } 
      {    old object    } <== list[0]

x==>{brand new object}通过执行第3行中的操作。实际上,您正在为第4行该范围内的名称
x
分配一个全新的值。你到底想做什么?在JS中无法存储指向数组成员的指针。您必须使用
list[0]={name:'baz'}
@Asad或
list[0]。name='baz'@Pinocchio,仅替换单个属性。我假设实际对象中还有其他属性,否则OP没有理由不能使用第3行中的属性。使用您建议的语法是没有意义的,它比您在第3行所做的等效的
x.name='baz'
长。实际上,您正在为第4行该范围内的名称
x
分配一个全新的值。你到底想做什么?在JS中无法存储指向数组成员的指针。您必须使用
list[0]={name:'baz'}
@Asad或
list[0]。name='baz'@Pinocchio,仅替换单个属性。我假设实际对象中还有其他属性,否则OP没有理由不能使用第3行中的属性。使用您建议的语法没有意义,它只比等效的
x.name='baz'
长。