1

I stumbled upon the following code:

obj = {};  // global object

function set(path, value) {
    var schema = obj;  // a moving reference to internal objects within obj
    var pList = path.split('.');
    var len = pList.length;
    for(var i = 0; i < len-1; i++) {
        var elem = pList[i];
        if( !schema[elem] ) schema[elem] = {}
        schema = schema[elem];
    }

    schema[pList[len-1]] = value;
}

set('mongo.db.user', 'root');

I read a bit further on it and found that schema is acting as a pointer of obj. How does this work? I even re-wrote the above code to incorporate ES6:

console.log(set('mongo.db.user', 'root'));

function set(path, data = '', obj = {}, delim = '.') {
  if (path) {;
    path.split(delim).reduce((s, n, i, a) => {
      s.pointer[n] = s.pointer[n] || i === a.length - 1 ? data : {};
      s.pointer = s.pointer[n];
      return s;
    }, {pointer: obj});
  }
  return obj;
}

However I feel limited in my ability to expand upon this because I do not see how, for example, if pointer is obj, how does setting s.pointer[n] set object, but s.pointer = s.pointer[n] not set/overwrite obj?

If someone could please explain this that would be great - also if I am missing something from the bigger picture (and thus didn't ask) please let me know.

4lackof
  • 1,250
  • 14
  • 33
  • There are no pointers. So "pointers don't work" in ECMAScript. The "secret" is this: 1) *an object is **always** itself* (so anything that modifies the object .. modifies the object), and 2) *assignment and parameter passing **never** copies/clones objects* (so an assigned/passed object is .. the same object) – user2864740 Jul 28 '18 at 19:56
  • (Unfortunately people like to bring in concepts like 'references' from *other* languages; these are not required to understand the simple-once-you-know-the-two-rules behavior. Although, Jon Skeet [??] had a good House Address analogy somewhere..) – user2864740 Jul 28 '18 at 20:03
  • I think one final thing to help reduce confusion is to consider variables are simply *names* for objects, and an object (like a person) may be known by one *or more* names. Thus, a variable in JavaScript does *not* "store a reference" (again, there are no 'references' from other mainstream languages present in JS), but rather, "a variable *names* an object" or - by English equivalence of terms - "a variable *refers to* an object". – user2864740 Jul 28 '18 at 20:11
  • Java seems to have been one of the main perpetrators at leaking implementation.. but ECMAScript/JavaScript != Java. – user2864740 Jul 28 '18 at 20:15
  • I disagree to the comments above. Yes, the term "pointers" was not coined in JS, instead its called a "reference", but thats exactly what the OP describes here, so the question is actually "How do references work in JS"? – Jonas Wilms Jul 28 '18 at 20:46
  • That said, I think you will find a lot of great explanations under the term "call by sharing", but this still remains a great question, so if there is a something in the duplicate that you don't understand I will be glad to reopen / answer / help :) – Jonas Wilms Jul 28 '18 at 20:49

0 Answers0