为什么下面的工作?
<something>.stop().animate(
{ 'top' : 10 }, 10
);
然而这是行不通的:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
更清楚地说:目前我还不能将CSS属性作为变量传递给动画函数。
为什么下面的工作?
<something>.stop().animate(
{ 'top' : 10 }, 10
);
然而这是行不通的:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
更清楚地说:目前我还不能将CSS属性作为变量传递给动画函数。
当前回答
你也可以这样尝试:
Const arr = [{ “描述”:“星期四”, “数”:“1”, “日期”:“2019-12-05” }, { “描述”:“星期三”, “数”:“0”, “日期”:“2019-12-04” }) Const res = arr。Map (value => { 返回{[value.description]: {count: value。计数、日期:值。日期 }) console.log (res);
其他回答
使用ECMAScript 2015,你现在可以直接在对象声明中使用括号表示:
var obj = {
[key]: value
}
其中key可以是返回值的任何类型的表达式(例如变量)。
你的代码看起来是这样的:
<something>.stop().animate({
[thetop]: 10
}, 10)
在被用作键之前,top将被计算。
你可以这样做:
var thetop = 'top';
<something>.stop().animate(
new function() {this[thetop] = 10;}, 10
);
你也可以这样尝试:
Const arr = [{ “描述”:“星期四”, “数”:“1”, “日期”:“2019-12-05” }, { “描述”:“星期三”, “数”:“0”, “日期”:“2019-12-04” }) Const res = arr。Map (value => { 返回{[value.description]: {count: value。计数、日期:值。日期 }) console.log (res);
ES5引用说它不应该工作
注意:ES6的规则已经更改:https://stackoverflow.com/a/2274327/895245
规范:http://www.ecma-international.org/ecma-262/5.1/ # sec-11.1.5
PropertyName : IdentifierName StringLiteral NumericLiteral [...] The production PropertyName : IdentifierName is evaluated as follows: Return the String value containing the same sequence of characters as the IdentifierName. The production PropertyName : StringLiteral is evaluated as follows: Return the SV [String value] of the StringLiteral. The production PropertyName : NumericLiteral is evaluated as follows: Let nbr be the result of forming the value of the NumericLiteral. Return ToString(nbr).
这意味着:
{theTop: 10}与{'theTop': 10}完全相同 PropertyName theTop是一个IdentifierName,所以它被转换为'theTop'字符串值,这是'theTop'的字符串值。 不可能用变量键编写对象初始化式(字面量)。 仅有的三个选项是IdentifierName(扩展为字符串文字),StringLiteral和NumericLiteral(也扩展为字符串)。
如果你想要对象键与变量名相同,在es2015有一个简短的手。 ECMAScript 2015中的新符号
var thetop = 10;
var obj = { thetop };
console.log(obj.thetop); // print 10