-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change isPlainObject to support objects created in a different context #3068
Change isPlainObject to support objects created in a different context #3068
Conversation
export default (x: any): boolean => | ||
x !== null && | ||
typeof x === 'object' && | ||
x.toString() === '[object Object]' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like indicated in the PR, this will need to be Object.prototype.toString
to support objects with toStrung
, including Object.create(null)
🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes of course, we can't call toString()
if it doesn't exist on that object.
The problem I was trying to address by using x.toString()
instead of Object.prototype.toString.call(x)
was to call a custom toString
when it exists:
class MyClass {
toString() {
return "[object MyClass]"
}
}
const m = new MyClass();
Object.prototype.toString.call(m); // "[object Object]" <-- does not call the custom toString method
m.toString(); // "[object MyClass]"
I can solve this by using: (x.toString ? x.toString() : Object.prototype.toString.call(x)) === '[object Object]'
Hi @kitten, are you happy with this now that it has been updated? |
Looking forward to seeing this in action! 👏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
To resolve #3066