Skip to content

proposal: syscall/js: method equivalent of "in" operator #73443

@jcbhmr

Description

@jcbhmr

Proposal Details

In JS I can do this:

function hello(options) {
  if ("name" in options) {
    if (options.name === undefined) { // could be "== null" to handle null & undefined
      console.log(`You've opted to not provide your name`)
    } else if (typeof options.name === "string") {
      console.log(`Hello ${options.name}!`)
    } else {
      throw new TypeError("name is not a string")
    }
  } else {
    console.log(`Name field is empty. Would you like to set it now?`)
    // blah blah
  }
}

This example illustrates the potential difference in semantics between a property being not present on an object and being undefined on an object.

You cannot currently detect the presence or absence of a property using syscall/js without resorting to calling Reflect.has(object, propertyName) via js.Global().Get("Reflect")...

I propose adding a Value.Has or similar method to expose the JS in operator similar to how instanceof is exposed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions