TS Failed to Use hasOwnProperty Method [How to Fix]

Determine whether the obj object contains the children attribute, but the following error occurs

Do not access Object.prototype method ‘hasOwnProperty’ from target object.

Cannot directly access methods on Object prototype

Reason:

It is not safe to call hasOwnProperty directly on the result object,If the obj object itself has the hasOwnProperty property,there will be some unexpected problems when calling it. If someone with intentions takes advantage of ,set the property in obj to malicious code, will cause some security issues.

Workaround:

Use call to change the call point

const hasChild = Object.prototype.hasOwnProperty.call(obj, 'children');

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *