0

If I've a variable and I want to know what exactly is the type of object, how would I do that?

// this prints "object"
// any way to know what kind of object
// like whether a GlideRecord or Reference Field or array or json?
gs.log(typeof myVariable);
asprin
  • 9,579
  • 12
  • 66
  • 119
  • 1
    What you're asking is, strictly-speaking, impossible (because JavaScript is structurally-typed, not *nominatively-typed*). Objects in JavaScript don't need a specific prototype or constructor to conform to a TypeScript interface or `type` definition, for example. – Dai Sep 30 '20 at 10:43

1 Answers1

0

If GlideRecord and so on are classes or constructor functions, you can use the constructor property.

class GlideRecord {}
// or legacy constructor function - 
// function GlideRecord() {}

const instance = new GlideRecord()

const { constructor } = instance

console.log(constructor)                 // GlideRecord
console.log(constructor.name)            // "GlideRecord"
console.log(constructor === GlideRecord) // true

Note that this will not work if they're just plain old JS objects created with factory functions - in this case, constructor will simply be Object:

const createGlideRecord = () => {
    return { /* ... */ }
}

const instance = createGlideRecord()

const { constructor } = instance

console.log(constructor)                       // Object
console.log(constructor.name)                  // "Object"
console.log(constructor === createGlideRecord) // false
Lionel Rowe
  • 5,164
  • 1
  • 14
  • 27