ohasOwnPropertya
The hasOwnProperty method is a built-in JavaScript function used to determine whether an object has a specified property as its own property (not inherited). This method is particularly useful when working with objects and inheritance, as it helps to distinguish between properties that belong directly to an object and those that are inherited from its prototype chain.
The hasOwnProperty method is called on an object and takes a single argument, which is the name
Here is an example of how to use the hasOwnProperty method:
};
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('b')); // true
console.log(obj.hasOwnProperty('toString')); // false
```
In this example, the object obj has properties 'a' and 'b', which are its own properties. The
The hasOwnProperty method is particularly useful in scenarios where you need to iterate over an object's
if (obj.hasOwnProperty(key)) {
console.log(key); // Outputs 'a' and 'b'
}
}
```
This ensures that only the object's own properties are processed, avoiding any properties inherited from the