javascript - Is a function return value possible as an array key? -
i made lazy utility function wanted pass arrays key getting syntax errors, possible pass function inside array it's key?
function encloseattrselector(attr, value) { return '[' + attr + '="' + value + '"]'; }
..
example (typically more 1 pair):
var data = { encloseattrselector('name', 'username'): row.first().text() };
in es6 es2015 (the newest official standard language) yes, in real-life contexts no. can 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 property name in object initializer expression. inside square brackets can expression.
Comments
Post a Comment