Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Suggestion: operator() and operator new #52

Open
@yw662

Description

@yw662

An operator() can make an object become a Callable with a function signature without actually make it a function.
And a similar operator new is also suggest to make the object a constructor.

like this:

const foo = {
  value: 1,
  bar: {
    value: 2,
    [Symbol.operatorSet]: Operators.create({
      "()"(thisArg, callee, ...args) {
        return thisArg.value + callee.value + args[0]
      },
      "new"(callee, ...args) {
        return Object.create(callee)
      }
    })
  }
}

foo.bar(3) // 6
new foo.bar() // { [[prototype]]: foo }

(It might be a bad idea to actually allow the same object to have different operator() and operator new, but, who knows.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions