In Javascript a Key is a Symbol
If you want to build at runtime an object with variable keys and values, remember that in Javascript, a key is actually a primitive symbol-value that doesn't get evaluated.
var f1 = function(key, value) {
return {key: value};
}
var f2 = function(key, value) {
var _o = {key: value};
return _o;
}
var f3 = function(key, value) {
var _o = {};
_o[key] = value;
return _o;
}
f1('hi', 'hello'); // -> {key: 'hello'};
f2('hi', 'hello'); // -> {key: 'hello'};
f3('hi', 'hello'); // -> {'hi': 'hello'};Written by Bogdan Ivanov
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#