简体   繁体   English

为数组内的变量赋值

[英]Assigning Values to Variables inside of an Array

I'm pretty new to coding and can't find out why this is outputting undefined:我对编码很陌生,无法找出为什么输出未定义:

var var0, list = [var0];
list[0] = true;
console.log(var0)

On line 1 you copy the value of var0 into the array.在第 1 行,您将var0复制到数组中。

On line 2 you replace the value in the array.在第 2 行,您替换数组中的

This doesn't have any effect on var0 .这对var0没有任何影响。 That is just a variable that used to have a copy of the same value .那只是一个曾经拥有相同副本变量 It is not a reference.它不是参考。

You never use or define the value of var0.您从不使用或定义 var0 的值。

list[0] = true;

This line replaces the value of the object in the 0 position (which is var0 because of the first line) to a boolean variable with the value "true".此行将 0 位置的对象值(由于第一行而为 var0)替换为值为“true”的布尔变量。

What you mean to do is你的意思是

var var0 = true, list = [var0];
console.log(list[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM