When you encounter the error “object.hasown is not a function,” it means that you are trying to use the “hasown” function/method on an object that does not have this function defined. This error commonly occurs when you mistakenly treat a non-function as a function.
Let’s take a closer look at this error with an example:
<script>
var person = {
name: "John",
age: 25
};
if (person.hasown("name")) {
console.log("Person has a 'name' property.");
}
</script>
In the above example, we have an object called “person” with properties “name” and “age”. The intention is to check if the object has a property called “name” using the “hasown” method. However, the error occurs because “hasown” is not a valid method for JavaScript objects.
The correct method to check for object properties is “hasOwnProperty()”. Here’s the corrected code:
<script>
var person = {
name: "John",
age: 25
};
if (person.hasOwnProperty("name")) {
console.log("Person has a 'name' property.");
}
</script>
In this updated code, we replace the incorrect “hasown” with the correct “hasOwnProperty” method. Now, the code will correctly check if the object “person” has a property called “name” and execute the associated code block if true.
Remember to check the documentation or resources for the specific object or method you are using to avoid such errors.