0

This is a general / fundamental question to help me understand the technical background of Objects in Javascript.

Coming from Java, I'm used to Objects (conceptually) being a collection of variables and methods. In Javascript however you can also access properties like a Python-like dictionary.

obj.property
obj['property']

In general this makes sense, as objects are essentially a meaningful collection of key/value pairs.

However, I wonder how it is actually implemented. Is it a hash-table, a datastructure or is it "normal" variables but with an alternative way to access them? For example, Java syntax suggests that properties are normal varibales when declaring a class like this:

class A {
    int a = 1;
    int b = 2;
}

In JS you actually never declare a property with let or const. Does this mean the property is actually just being pushed into some data-structure / collection?

tweekz
  • 61
  • 6
  • For the purpose of comparison, yeah, I would say it's a Hash Table, – Keith Feb 01 '22 at 11:50
  • Your Java code for `class A` doesn't declare "variables" and is thus not equivalent at all to a `let` or `const` in JS. It's declaring class fields and sets their scope to default (package-scope). E.g., another class might do `class B { public int foo = 42; }` where the field is marked `public`. It's not a variable like the one you'd declare in a method, e.g., `void test() { int a = 1; }` is *completely different* from the class field. – VLAZ Feb 01 '22 at 11:52

0 Answers0