是否可以使用一个参数来创建一个新的对象键,并将其作为同一个函数中的变量?

时间:2022-09-25 18:48:08

This is obviously a totally inefficient way to change the background color of a button, but I'm wondering why this doesn't work:

这显然是一种完全低效的方式来改变按钮的背景颜色,但我想知道为什么它不起作用:

<button id="blueButton">Button</button>
var data = {};

function changeColor(e){
  data.e = "blue";
  $('#' + e).css('background-color', data.e);
}

changeColor(blueButton);

If a variable is able to be used inside of a string (e.g. ${variable}) why wouldn't it be able to be used in the above scenario?

如果一个变量可以在字符串中使用(例如${variable}),为什么不能在上面的场景中使用它呢?

2 个解决方案

#1


5  

To set the key of an object by a variable you need to use bracket notation: Keep in mind that javascript allows only string or Symbol as Object key. If you want to use some other type to key you need to have a look at Map

要通过变量设置对象的键,需要使用方括号表示法:记住javascript只允许字符串或符号作为对象键。如果你想使用其他类型的键,你需要看看地图

var data = {};

function changeColor(e){
  data[e] = "blue";
  $('#' + e).css('background-color', data[e]);
}

changeColor(blueButton);

#2


0  

I think you need to store changed background color of each button id. below code should be helps to you. check console.log

我认为你需要存储每个按钮的背景颜色变化。下面的代码应该对你有所帮助。检查console.log

var data = {};

function changeColor(e){
  data[e] = "blue";
  $('#' + e).css('background-color', data[e]);
}

changeColor('btn1');
console.log(data);
changeColor('btn2');
console.log(data);
changeColor('btn3');
console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="btn1">Button1</button>
<button id="btn2">Button2</button>
<button id="btn3">Button3</button>

In your code, seems you have passed id as object not a string. data.e, where e is property of data it is not passed parameters value.

在代码中,似乎已经将id作为对象而不是字符串传递。数据。e是数据的属性,它没有传递参数值。

#1


5  

To set the key of an object by a variable you need to use bracket notation: Keep in mind that javascript allows only string or Symbol as Object key. If you want to use some other type to key you need to have a look at Map

要通过变量设置对象的键,需要使用方括号表示法:记住javascript只允许字符串或符号作为对象键。如果你想使用其他类型的键,你需要看看地图

var data = {};

function changeColor(e){
  data[e] = "blue";
  $('#' + e).css('background-color', data[e]);
}

changeColor(blueButton);

#2


0  

I think you need to store changed background color of each button id. below code should be helps to you. check console.log

我认为你需要存储每个按钮的背景颜色变化。下面的代码应该对你有所帮助。检查console.log

var data = {};

function changeColor(e){
  data[e] = "blue";
  $('#' + e).css('background-color', data[e]);
}

changeColor('btn1');
console.log(data);
changeColor('btn2');
console.log(data);
changeColor('btn3');
console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="btn1">Button1</button>
<button id="btn2">Button2</button>
<button id="btn3">Button3</button>

In your code, seems you have passed id as object not a string. data.e, where e is property of data it is not passed parameters value.

在代码中,似乎已经将id作为对象而不是字符串传递。数据。e是数据的属性,它没有传递参数值。