Non-persistent storage for key-value pairs I got an interesting interview question related to JavaScript:


  In JavaScript please create a storing function called storingFn with the following characteristics:
  
  Read Cafefully
  
  
  Save immutable key-values. 
  The values can only be accessed by storingFn and should not be exposed in the global scope. 
  It can't use Browser storage (localStorage, sessionStorage), cookies, global scope or any other object that is part of the Browser.
  The only global function for this implementation is storingFn (Ignore the assert function). 
  And it doesn't matter if the key-values don't persist if you refresh the browser.
  


I wrote an IIFE as a solution. What do you think of it? I couldn't find any solution online, which makes this question very interesting for me.

var storingFn = (function(){
  var immObj = {};

  function set(key, value){
    // check if key already exist
    if (key in immObj){
      return false;
    }

    // it's a new key , assign it
    immObj[key]  = value;
    return true;
  }

  // verbose but looks better organize
  function get(key){
    return immObj[key];
  }

  var initStore = function(key, value){
    //console.log(immObj);
    // if value is defined
    if (typeof value != 'undefined'){
      return set(key, value);
    }
    // if value is not defined
    else {
      return get(key);
    }
  }

  return initStore;
})();

storingFn('name', 'Nic') //Should return true
storingFn('name') // Should return "Nic"'
storingFn('name', 'Robert') //Should return false
You didn't say if you were able to use ES6 or not, but here's how I would do it if you can.  Note this solution ensures that you won't return or overwrite values that are on the Object prototype of the storage. It's also somewhat more terse, if you get rid of my comments.  

const storingFn = (function(){
  const store = {};

  return (key, value) =&gt; {
    // you only want to return things that are on 'store' not on its ancestral prototype. 
    if(store.hasOwnProperty(key)) {
      return store[key];
    }

    // If we get here, we know it's a set operation, so I'm assuming we need to quietly avoid setting null keys. Throw an error if you prefer. 
    if (value === 'undefined') {
      return null;
    }
    // I didn't test with &lt; ES6, as written it doesn't allow overwrite of 'toString' for example.  
    store[key] = value;
    return value;
  }
})();


EDIT: 

I also decided to actually return the value in the key, regardless of whether we are setting or getting, as your question didn't specify which was appropriate and I thought this gave a more consistent interface.

If you need to return false if the key is already set (right now it'll return the unchanged value) then that's a simple change to the first conditional block.