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 () {});
this part causes the error:
self.settings.direction
any ideas where my syntax is wrong? thank you
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.
You need to make the object first, then use [] to set it. var key = "happyCount"; var obj = {}; obj[key] = someValueArray; myArray. push(obj);
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 () {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With