为什么下面的工作?
<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属性作为变量传递给动画函数。
当前回答
ES5实现分配键如下:
var obj = Object.create(null),
objArgs = (
(objArgs = {}),
(objArgs.someKey = {
value: 'someValue'
}), objArgs);
Object.defineProperties(obj, objArgs);
我附上了一个片段,我用来转换为裸对象。
var obj = { 'key1': 'value1', 'key2': 'value2', 'key3': [ 'value3', 'value4', ], 'key4': { 'key5': 'value5' } } var bareObj = function(obj) { var objArgs, bareObj = Object.create(null); Object.entries(obj).forEach(function([key, value]) { var objArgs = ( (objArgs = {}), (objArgs[key] = { value: value }), objArgs); Object.defineProperties(bareObj, objArgs); }); return { input: obj, output: bareObj }; }(obj); if (!Object.entries) { Object.entries = function(obj){ var arr = []; Object.keys(obj).forEach(function(key){ arr.push([key, obj[key]]); }); return arr; } } console(bareObj);
其他回答
2020年更新/ example.com ...
一个更复杂的例子,使用括号和字面量…您可能需要做一些事情,例如使用vue/axios。把文字用括号括起来,所以
['…]']
{
[`filter[${query.key}]`]: query.value, // 'filter[foo]' : 'bar'
}
{thetop: 10}是一个有效的对象字面值。这段代码将创建一个属性为thetop的对象,值为10。以下两种情况相同:
obj = { thetop : 10 };
obj = { "thetop" : 10 };
在ES5及更早的版本中,不能在对象文字中使用变量作为属性名。你唯一的选择是做以下事情:
var thetop = "top";
// create the object literal
var aniArgs = {};
// Assign the variable property name with a value of 10
aniArgs[thetop] = 10;
// Pass the resulting object to the animate method
<something>.stop().animate(
aniArgs, 10
);
ES6将ComputedPropertyName定义为对象字面量语法的一部分,这允许你像这样编写代码:
var thetop = "top",
obj = { [thetop]: 10 };
console.log(obj.top); // -> 10
您可以在每个主流浏览器的最新版本中使用这种新语法。
给定的代码:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
翻译:
var thetop = 'top';
var config = { thetop : 10 }; // config.thetop = 10
<something>.stop().animate(config, 10);
正如你所看到的,{thetop: 10}声明没有使用变量thetop。相反,它创建了一个具有名为thetop键的对象。如果你想让键值是顶部变量的值,那么你必须在顶部使用方括号:
var thetop = 'top';
var config = { [thetop] : 10 }; // config.top = 10
<something>.stop().animate(config, 10);
方括号语法已在ES6中引入。在JavaScript的早期版本中,您必须执行以下操作:
var thetop = 'top';
var config = (
obj = {},
obj['' + thetop] = 10,
obj
); // config.top = 10
<something>.stop().animate(config, 10);
你可以这样做:
var thetop = 'top';
<something>.stop().animate(
new function() {this[thetop] = 10;}, 10
);
ES5实现分配键如下:
var obj = Object.create(null),
objArgs = (
(objArgs = {}),
(objArgs.someKey = {
value: 'someValue'
}), objArgs);
Object.defineProperties(obj, objArgs);
我附上了一个片段,我用来转换为裸对象。
var obj = { 'key1': 'value1', 'key2': 'value2', 'key3': [ 'value3', 'value4', ], 'key4': { 'key5': 'value5' } } var bareObj = function(obj) { var objArgs, bareObj = Object.create(null); Object.entries(obj).forEach(function([key, value]) { var objArgs = ( (objArgs = {}), (objArgs[key] = { value: value }), objArgs); Object.defineProperties(bareObj, objArgs); }); return { input: obj, output: bareObj }; }(obj); if (!Object.entries) { Object.entries = function(obj){ var arr = []; Object.keys(obj).forEach(function(key){ arr.push([key, obj[key]]); }); return arr; } } console(bareObj);