Skip to content

Should handling nullish values and null-prototype objects be the focus? #19

Description

@mfulton26

An extensions syntax may not be necessary if nullish values and null-prototype objects do not need to be handled or if something is made for handling them.

e.g. Symbols can be used to implement code similar to that in the example in README.md:

function x(attributes) {
    const key = Symbol()
    Object.defineProperty(Object.prototype, key, attributes)
    return key
}
// define two extension methods
const toArray = x({ value() { return [...this] } })
const toSet = x({ value() { return new Set(this) } })

// define a extension accessor
const allDivs = x({ get() { return this.querySelectorAll('div') } })

// reuse built-in prototype methods and accessors
const flatMap = x({ value: Array.prototype.flatMap })
const size = x(Object.getOwnPropertyDescriptor(Set.prototype, 'size'))

// Use extension methods and accessors to calculate
// the count of all classes of div element.
let classCount = document[allDivs]
	[flatMap](e => e.classList[toArray]())
	[toSet]()[size]

Symbol-keyed prototype augmentation does not suffer from the same problems as string-keyed prototype augmentation because symbols, unlike strings, are unique at runtime and will not conflicts with other augmentations nor future built-in definitions.

Using symbols here doesn't work for null or undefined (although nullish coalescing does work) nor does it work with null-prototype objects. That may be sufficient. If not, then perhaps a proposal focusing on handling those edge cases would be worthwhile.

Ideas: A global/root prototype that can only contain non-enumerable definitions using symbol keys.

function x(attributes) {
    const key = Symbol()
    Object.defineProperty(GlobalPrototype, key, attributes)
    return key
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions