<commit_before>## `hasOwnProperty`

If you want to check whether a `Object` has a property defined on **itself** (and 
not just somewhere on the [prototype chain](#prototype), you have to use the 
`hasOwnProperty` method which all objects inherit from `Object.prototype`.

While it is tempting to just do a `Foo.bar !== undefined` this is in no way safe.
Although JavaScript does return `undefined` for non-existent properties, they
property might very well exist, but it's value just happens to be set to 
`undefined`.

Additionally, using the `in` operator doesn't work either, since it **does** 
traverse the prototype chain upwards until it finds a property with the 
specified name.

    // Poisoning Object.prototype, NEVER do this, it calls for trouble
    Object.prototype.bar = 1; 
    var foo = {goo: undefined};
    
    foo.bar; // 1
    'bar' in foo; // true

    foo.hasOwnProperty('bar'); // false
    foo.hasOwnProperty('goo'); // true

As you can see, only `hasOwnProperty` gives the correct and expected result. 
`hasOwnProperty` is of essential use when iterating over the properties of any 
`Object`, since there's no other way to exclude things that are not defined on 
the object **itself** but rather somewhere on its prototype chain.  

### Best practices

When checking for the existence of a property on a object, `hasOwnProperty` is 
the only way to go. It's also recommended to make sure that `hasOwnProperty` is 
part of **every** [for in loop](#forinloop).
<commit_msg>Formatting, minor grammatical errors corrected<commit_after>## `hasOwnProperty`

If you want to check whether a `Object` has a property defined on **itself** (and 
not just somewhere on the [prototype chain](#prototype), you have to use the 
`hasOwnProperty` method which all objects inherit from `Object.prototype`.

While it is tempting to just do a `Foo.bar !== undefined`, this is in no way safe.
Although JavaScript does return `undefined` for non-existent properties, they
property might very well exist, but its value just happens to be set to 
`undefined`.

Additionally, using the `in` operator doesn't work either, since it **does** 
traverse the prototype chain upwards until it finds a property with the 
specified name.

    // Poisoning Object.prototype, NEVER do this, it calls for trouble
    Object.prototype.bar = 1; 
    var foo = {goo: undefined};
    
    foo.bar; // 1
    'bar' in foo; // true

    foo.hasOwnProperty('bar'); // false
    foo.hasOwnProperty('goo'); // true

As you can see, only `hasOwnProperty` gives the correct and expected result. 
`hasOwnProperty` essential when iterating over the properties of any 
`Object`, since there's no other way to exclude things that are not defined on 
the object **itself** but rather somewhere on its prototype chain.  

### Best practices

When checking for the existence of a property on a object, `hasOwnProperty` is 
the only way to go. It's also recommended to make sure that `hasOwnProperty` is 
part of **every** [`for in` loop](#forinloop).

{'fraction_non_alphanumeric': 0.08435329143235197, 'fraction_numerical': 0.0013231888852133643, 'mean_word_length': 4.023255813953488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4586555', 'n_tokens_mistral': 821, 'n_tokens_neox': 798, 'n_words': 421}