I made a lazy utility function that I wanted to pass as my arrays key but I was getting syntax errors, is it possible to pass a function inside the array as it's key?
function encloseAttrSelector(attr, value)
{
return '[' + attr + '="' + value + '"]';
}
..
example (typically more than one pair):
var data = { encloseAttrSelector('name', 'username'): row.first().text() };
In ES6 ES2015 (the newest official standard for the language) yes, but in most real-life contexts no. You can however do this:
var data = {};
data[encloseAttrSelector('name', 'username')] = row.first().text();
The new ES2015 syntax is:
var data = { [encloseAttrSelector('name', 'username')] : row.first().text() };
That is, square brackets around what would normally be just the property name in an object initializer expression. Inside the square brackets can be any expression.
For more than one pair, please do this:
var data = {};
data[encloseAttrSelector('name', 'username')] = row.first().text();
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