Is it possible to use ES6 Symbols in LocalStorageStoring a Symbol from React state in localStorage loads as “undefined”Storing Objects in HTML5 localStorageWhat is the max size of localStorage values?How do I store an array in localStorage?Clearing localStorage in javascript?Map vs Object in JavaScriptWhat is the motivation for bringing Symbols to ES6?ES6 class variable alternativesIs it possible to apply CSS to half of a character?Using Node.js require vs. ES6 import/exportWhen should I use curly braces for ES6 import?
Is there an idiom that means "accepting a bad business deal out of desperation"?
How can I minimize the damage of an unstable nuclear reactor to the surrounding area?
One word for 'the thing that attracts me'?
Gravitational Force Between Numbers
Why was this character made Grand Maester?
Are PMR446 walkie-talkies legal in Switzerland?
Visual Block Mode edit with sequential number
How did the Allies achieve air superiority on Sicily?
Are there historical examples of audiences drawn to a work that was "so bad it's good"?
Why did OJ Simpson's trial take 9 months?
Are cells guaranteed to get at least one mitochondrion when they divide?
Moons and messages
Count all vowels in string
These Two Cubes are The Only Ones That Are All Pure Prime..name them
What did the 'turbo' button actually do?
ifconfig shows UP while ip link shows DOWN
Goldfish unresponsive, what should I do?
Storing voxels for a voxel Engine in C++
Are there guidelines for finding good names for LaTeX 2e packages and control sequences defined in these packages?
Piping the output of comand columns
What is Orcus doing with Mind Flayers in the art on the last page of Volo's Guide to Monsters?
Why is 'additive' EQ more difficult to use than 'subtractive'?
How to escape dependency hell?
Can a multiclassed Kensei monk/Swashbuckler rogue use an offhand finesse weapon to trigger Sneak Attack, without using a bonus action?
Is it possible to use ES6 Symbols in LocalStorage
Storing a Symbol from React state in localStorage loads as “undefined”Storing Objects in HTML5 localStorageWhat is the max size of localStorage values?How do I store an array in localStorage?Clearing localStorage in javascript?Map vs Object in JavaScriptWhat is the motivation for bringing Symbols to ES6?ES6 class variable alternativesIs it possible to apply CSS to half of a character?Using Node.js require vs. ES6 import/exportWhen should I use curly braces for ES6 import?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When using a ES6 Symbol
as the key for a key/value pair in LocalStorage
, can we then still access it after reloading the page?
I found this tutorial that claims this to be possible when using Symbol.for
, but so far I have no success and get an undefined
when I try to retrieve the LocalStorage key/value pair.
As a side question: does it make sense to use Symbols
over here?
javascript ecmascript-6 local-storage
add a comment |
When using a ES6 Symbol
as the key for a key/value pair in LocalStorage
, can we then still access it after reloading the page?
I found this tutorial that claims this to be possible when using Symbol.for
, but so far I have no success and get an undefined
when I try to retrieve the LocalStorage key/value pair.
As a side question: does it make sense to use Symbols
over here?
javascript ecmascript-6 local-storage
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46
add a comment |
When using a ES6 Symbol
as the key for a key/value pair in LocalStorage
, can we then still access it after reloading the page?
I found this tutorial that claims this to be possible when using Symbol.for
, but so far I have no success and get an undefined
when I try to retrieve the LocalStorage key/value pair.
As a side question: does it make sense to use Symbols
over here?
javascript ecmascript-6 local-storage
When using a ES6 Symbol
as the key for a key/value pair in LocalStorage
, can we then still access it after reloading the page?
I found this tutorial that claims this to be possible when using Symbol.for
, but so far I have no success and get an undefined
when I try to retrieve the LocalStorage key/value pair.
As a side question: does it make sense to use Symbols
over here?
javascript ecmascript-6 local-storage
javascript ecmascript-6 local-storage
asked Nov 13 '17 at 11:56
sjbuyssesjbuysse
1,0312820
1,0312820
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46
add a comment |
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46
add a comment |
1 Answer
1
active
oldest
votes
You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage
is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem()
.
Unfortunately localStorage
doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...
You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString()
representation:
const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))
[ Check it out on jsbin.com ]
As a side question: does it make sense to use Symbols over here?
I suppose you could use reserved Symbols (e.g. Symbol.iterator
) to extend the functionality of the localStorage
global. But that's kind of besides the point.
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47263812%2fis-it-possible-to-use-es6-symbols-in-localstorage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage
is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem()
.
Unfortunately localStorage
doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...
You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString()
representation:
const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))
[ Check it out on jsbin.com ]
As a side question: does it make sense to use Symbols over here?
I suppose you could use reserved Symbols (e.g. Symbol.iterator
) to extend the functionality of the localStorage
global. But that's kind of besides the point.
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
add a comment |
You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage
is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem()
.
Unfortunately localStorage
doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...
You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString()
representation:
const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))
[ Check it out on jsbin.com ]
As a side question: does it make sense to use Symbols over here?
I suppose you could use reserved Symbols (e.g. Symbol.iterator
) to extend the functionality of the localStorage
global. But that's kind of besides the point.
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
add a comment |
You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage
is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem()
.
Unfortunately localStorage
doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...
You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString()
representation:
const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))
[ Check it out on jsbin.com ]
As a side question: does it make sense to use Symbols over here?
I suppose you could use reserved Symbols (e.g. Symbol.iterator
) to extend the functionality of the localStorage
global. But that's kind of besides the point.
You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage
is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem()
.
Unfortunately localStorage
doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...
You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString()
representation:
const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))
[ Check it out on jsbin.com ]
As a side question: does it make sense to use Symbols over here?
I suppose you could use reserved Symbols (e.g. Symbol.iterator
) to extend the functionality of the localStorage
global. But that's kind of besides the point.
edited Nov 13 '17 at 17:25
answered Nov 13 '17 at 14:49
sdglucksdgluck
10.6k13051
10.6k13051
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
add a comment |
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?
– sjbuysse
Nov 13 '17 at 15:20
1
1
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
@sjbuysse None. Which is why you should not do this.
– Bergi
Nov 13 '17 at 15:57
3
3
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.
– sdgluck
Nov 13 '17 at 16:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47263812%2fis-it-possible-to-use-es6-symbols-in-localstorage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you share an example where it is not working? For example, on jsbin.com.
– sdgluck
Nov 13 '17 at 14:46