26

I was looking for a way to check if an object is a FormData instance, similarly to Array.isArray()

Edit: If someone knows about instanceof, they obviously won't need to be pointed to instanceof. Obviously not a duplicate.

km6
  • 1,842
  • 2
  • 12
  • 18

1 Answers1

45

Use instanceof

For example:

let formData = new FormData()
let time = new Date()

console.log("statement: formData is a FormData instance", formData instanceof FormData) // statement is true
console.log("statement: time is a FormData instance", time instanceof FormData) // statment is false

Source

km6
  • 1,842
  • 2
  • 12
  • 18
  • 2
    The reason there's an *isArray* mehtod is that *instanceof* does not work across frames (or global environments) where the constructor is in one environment but the object was created in the other. You seem to have dismissed *instanceof* in your OP, then post an answer using it. What's the point? – RobG Sep 11 '17 at 00:58