-3

As python memory model is dictionaries of dictionaries, looks like any object in JavaScript has similar representation.

Below code,

>
foo = {}

makes foo as an empty dictionary {}. fine.

If code says,

> foo["bar"] = function(){
     this.ack = 3;
  };

then foo looks like a dictionary as shown below:

{"bar": function(){this.ack = 3}, "__proto__":Object}

Is my understanding correct?

Is yes, then any object in JavaScript can be accessed as object[expression], in my case object is foo.

mdn also says: JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name and a value.

If I drill down the details of foo, representation of foo from browser console, looks like dictionaries of dictionaries as shown below(in screenshot):

enter image description here

My question:

Can you write the dictionary form {} of foo for the above representation(shown in screenshot) as it looks like dictionaries of dictionaries ?

To support my claim that, objects are dictionaries in JavaScript, below is the data.

I observed that, anything is stored as dictionary element, in JavaScript:

var x = "hello";
window.x["length"]
   5
var number = 2
   undefined
number["toString"]
   toString()
number["toString"]();
   "2"

enter image description here

I could write any code in JavaScript in terms of dictionary.

var foo = "hello";
var x = foo.length;  //5
x = foo.charAt(1);    // "e"
x = "avast".charAt(3);  //"s"
//like this
window["foo"] = "hello";
    "hello"
window["x"] = window["foo"].length;
    5
window["x"]
    5
window["x"] = window["foo"].charAt(1)
    "e"
window["x"] = "avast".charAt(3);
    "s"

Note: key is always a string in Javascript. string is an immutable type

  • Can you write my foo in dictionary form {}? this is my question. I am not sure, if my question is so confusing!!! – overexchange Jun 3 '15 at 14:10
  • 2
    You are trying to understand the representation in the symbol table. In many instances, this is a hash table aka a dictionary. Some languages allow you closer (or direct) access to the underlying symbol table for a given thing. Debuggers often allow you to interrogate the symbols and objects more closely even if the language doesn't provide it. – user40980 Jun 3 '15 at 15:02
  • 1
    I didn't downvote, but I am having trouble understanding exactly what you're asking. First, I am having trouble parsing your question, "Can you write the dictionary form {} of foo for the above representation...?" Do you want to see an object literal that produces the same console output? Or are you asking if it is possible (vs. not possible) to do that at all? Second, I don't understand how your section "Infact, I observed that, anything is stored as dictionary element..." relates to the rest of your question. It seems like a totally separate observation. – apsillers Jun 3 '15 at 16:55
  • 1
    Just to be clear, I'm sure you do have a perfectly valid answerable question, but from reading your post, I'm not 100% sure what that question is right now. I could post an answer with information that will probably be helpful to you, but I'm not sure if would answer your exact question, because I don't fully understand it. – apsillers Jun 3 '15 at 16:56
  • I wrote foo as {"bar": function(){this.ack = 3}, "__proto__":Object} this is a dictionary, but I included only two elements who keys are bar and "proto", but in the screenshot you have many such key-value pairs. – overexchange Jun 3 '15 at 16:57
3

As you can see in the console, you're looking at bar, a function with the body shown. Within that are a number of properties available on function objects. If you care about how foo is stored, check out this article on V8's isomorphism and structure optimization.

Looking at the MDN docs for function, you can see the same list of properties. arguments represents the arguments the function was called with, caller is the function that did the calling, length is the function's arity, and name is the functions proper name.

Since you're looking at the function object from outside -- as opposed to a stack trace while the function is being executed -- many of those fields are not filled in. There are no arguments or caller to a function object until it has been called, unsurprisingly.

There is no "dictionary" for this. Javascript has some types with their own properties, inherited or instance, that are not simple dictionaries. You can define your own types using new and a constructor function, which behave like dictionaries with prototypical inheritance, but aren't actually simple dictionaries. The dictionary representation in Javascript is largely an artifact of old runtimes that may have used maps to store objects, and has been retained so you can consistently treat different object types using the same associative-array syntax for access.

If you were to recursively walk enumerable keys for the object you've created, it would look like:

var foo = {
  bar: function() {
    this.ack = 3;
  }
};

function walkKeys(obj) {
  return Object.keys(obj).map(function(key) {
    return {
      key: key,
      children: walkKeys(obj[key])
    };
  });
}

var keys = walkKeys(foo);
document.getElementById('r').textContent = JSON.stringify(keys);
<pre id=r></pre>

You don't get any keys for the function because it doesn't have any enumerable properties of its own. Those properties are all from the prototype or defined to be hidden and the function is not just a dictionary.

|improve this answer|||||
  • function is not just a dictionary? I do not agree. On browser console, If you try foo.bar["__defineGetter__"], this is one element in the dictionary. There are many such elements. – overexchange Jun 3 '15 at 15:30
  • Most JS objects act as associative arrays or maps, but are typically not implemented as such, at least not in modern VMs. – ssube Jun 3 '15 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.