I was looking for a way to check if an object is a FormData instance, similarly to Array.isArray()
Asked
Active
Viewed 2.1k times
28

km6
- 2,191
- 2
- 15
- 18
-
what is the question here? – hackerrdave Sep 11 '17 at 00:04
-
Possible duplicate of [What is the instanceof operator in JavaScript?](https://stackoverflow.com/questions/2449254/what-is-the-instanceof-operator-in-javascript) – hackerrdave Sep 11 '17 at 00:05
-
@hackerrdave how to detect if a JavaScript object is an instance of FormData – km6 Sep 11 '17 at 00:05
-
1@km6—then perhaps that question should be in the text of your question somewhere. – RobG Sep 11 '17 at 00:55
1 Answers
57
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

km6
- 2,191
- 2
- 15
- 18
-
3The 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
-
@RobG The main point is that instead of implying that my question was a duplicate of another one regarding the usage of `instanceof`, it should have been presented as an answer explaining how `instanceof` could solve my problem - which is exactly what I did in my own response. At the time of posting, I wasn't aware of `instanceof`, and that was the message I intended to convey in my original post. I didn't dismiss`instanceof`. – km6 May 15 '23 at 11:03