News for hackers. Stuff that matters.
Re:JavaScript's New Superpower: Explicit Resource Management by MrJohz on Saturday May 17, 2025 @10:42AM (#44014677)
Dynamic property access perhaps?

The premise is that you can always access an object's properties using indexing syntax as well as the normal dot syntax. So `object.foo` is the equivalent of `object["foo"]` or `object["f" + "o" + "o"]` (because the value inside the square brackets can be any expression). And if `object.foo` is a method, you can do `object.foo()` or `object ["foo"]()` or whatever else as well.

Normally, the key expression will always be coerced to a string, so if you did `object[2]`, this would be the equivalent of object["2"]. But there is an exception for symbols, which are a kind of unique object that is always compared by reference. Symbols can be used as keys just as they are, so if you do something like

    const obj = {}
    obj.foo = "bar"
    obj[Symbol("foo")] = "bar"
    console.log(obj)
You should see in the console that this object has a special key that is a symbol, as well as the normal "foo" attribute.

The last piece of the puzzle is that there are certain "well known symbols" that are mostly used for extending an object's behaviour, a bit like __dunder__ methods in Python. Symbol.dispose is one of these - it's a symbol that is globally accessible and always means the same thing, and can be used to define some new functionality without breaking backwards compatibility.

I hope that helps, feel free to ask more questions.

Re:JavaScript's New Superpower: Explicit Resource Management by cluckindan on Saturday May 17, 2025 @11:19AM (#44014955)
It’s not that, it’s a dynamic key in an object literal.

    const key = "foo";
    const obj = { [key]: "bar" };
    console.log(obj.foo); // prints "bar"
Re:JavaScript's New Superpower: Explicit Resource Management by MrJohz on Saturday May 17, 2025 @01:45PM (#44015816)
That's also possible, and it's common when using this pattern, but the specific syntax in the original question was I believe property access, and not part of a property literal. I didn't bring that up because I thought my comment was long enough and I wanted to explain that specific syntax. But yeah, you also have this syntax to set properties in object literals, and a similar syntax in classes.