mavoweb / treecle

WIP. A toolbox for hierarchical JS objects.
https://treecle.mavo.io
MIT License
6 stars 0 forks source link

Branching by type in a generic way #4

Open LeaVerou opened 6 months ago

LeaVerou commented 6 months ago

In the initial Treecle port, I removed all the functionality that supports type => something object literals and instead it only supports functions for these things. I instead added code to Vastly to transform object literals into functions.

But it just dawned on me: this would be very useful in Treecle too, it's just that we can't depend on node.type returning a useful type. But we could have a config.getType() method, which by default returns the object [[ Class ]], but can be overridden (e.g. Vastly would do config.getType = n => n.type).

Any objections @adamjanicki2 ?

adamjanicki2 commented 6 months ago

No objections here, I think that sounds good. What does [[ Class ]] mean?

LeaVerou commented 6 months ago

What does [[ Class ]] mean?

Warning: infodumping ahead 😅

When you do Object.prototype.toString.call(obj), you get a string like "[object Foo]". The Foo in there is the object’s internal type, which used to be called [[ Class ]]. You may still see "[[ Class ]] being used since AFAIK there is no better term to describe the concept).

Reading [[ Class ]] is generally more robust than typeof or instanceof, since:

In the past only native objects had the privilege of having a [[ Class ]] that was not "Object", but now authors can use Symbol.toStringTag to override the default.

[[ ]] denotes an internal slot, i.e. a property or method that is implemented by the JS runtime but cannot be accessed or modified directly. See https://medium.com/jspoint/what-are-internal-slots-and-internal-methods-in-javascript-f2f0f6b38de

adamjanicki2 commented 6 months ago

Following up on this, how do you get the [[ Class ]] for an object?

LeaVerou commented 6 months ago

Like this: https://github.com/mavoweb/treecle/blob/main/src/util.js#L19-L33

adamjanicki2 commented 6 months ago

Accidentally closed this