How do I check if a variable contains a certain property?
Let’s assume:
var x = 'some string';
Then I would like to do:
if ('trim' in x)
but that gives me a:
Uncaught TypeError: Cannot use 'in' operator to search for 'trim' in string
I can make it work by using:
'trim' in x.__proto__
but according to MDN __proto__
is deprecated and its use is (quote) “controversial and discouraged”.
How can I achieve this then?
I have tried to use Object.keys(x)
but that only lists the keys that are valid as indexes, not the properties.
I know that, in this specific example, I could simply use typeof x === 'string'
but that’s not a universal solution.