I don't really understand what's the question is. Is it :
- can I create a variable that directly points to ss.storage ?
- or can I access a storage member by using a function returning the name of that member ?
Short answer : Yes, you can and yes you can !
You can of course write without problem :
var ss = require("sdk/simple-storage");
var storage = ss.storage;
/* using 'storage' doesn't matter as long as you don't have identifyers collision */
storage[naming()] = new entryConstructor();
// and you still can refer to storage using ss.storage
ss.storage
is much like a javascript object. You just can't add functions members to it (if I'm not mistaken).
If you have a function that returns a string
, you can of course access/set a storage property/entry (that's how I like to name it) using that function, assuming the returned value is a javascript compliant member name :
ss.storage[naming()] = new entryConstructor();
where :
function naming() {
/* returns a valid javascript object property names */
// building of the property name here...
}
and entryConstructor
is one of the allowed types (presumably an object of your own creation) I like to think of storage entries as JSON serializable datas.
Be carefull when you set entries in ss.storage. You should check for the existence of the entry before attempting to set its value in case you want persistent datas across multiple sessions. If you start by setting values each time a session has begun, old values registered on previous sessions would get lost forever :
let name = naming();
let myEntry;
if (ss.storage.hasOwnProperty(name)) {
// use stored value...
myEntry = ss.storage[name];
} else {
// create value...
myEntry = new entryConstructor()...
}
// make edits... then :
ss.storage[name] = myEntry;
// or handle update on user input, contentScript detach or exports.onUnload
Side notes :
- carefull of syntax errors (missing parenthesis, etc.)
- When you talk about property global object (window), you're actually talking about context.