javascript对象变量键 [英] javascript object variable key

查看:106
本文介绍了javascript对象变量键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Jquery创建一个插件,并且需要将变量作为对象中的键。

I am creating a plugin for Jquery and need to have a variable as a key in an object.

$(selector).animate({self.settings.direction: '+='+self.displacement+'px'}, "slow" , function () {});

此部分导致错误:


self.settings.direction

self.settings.direction

我的语法错误的任何想法?
谢谢

any ideas where my syntax is wrong? thank you

推荐答案

AFAIK,你做不到。对象文字表示法中冒号前面的任何内容都将自动解释为字符串。您需要事先构造对象,并使用方括号表示法。

AFAIK, you can't. Whatever is in front of the colon in the object literal notation will be automatically interpreted as a string. You will need to construct your object beforehand, and use the square bracket notation.

var options = {}
options[self.settings.direction] = '+=' + self.displacement + 'px';
$(selector).animate(options, "slow" , function () {});

这篇关于javascript对象变量键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆