File tree 1 file changed +16
-0
lines changed
1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -269,6 +269,22 @@ <h1>JavaScript Garden</h1>
269
269
way to exclude properties that are not defined on the object < strong > itself</ strong > , but
270
270
somewhere on its prototype chain.< br />
271
271
</ p >
272
+ </ section > < section > < header > < h3 > < code > hasOwnProperty</ code > as a property</ h3 > </ header >
273
+ < p > JavaScript does not protect the property name < code > hasOwnProperty</ code > ; therefore, if the
274
+ possibility exists that an object might have a property with this name, it is
275
+ necessary to use an < em > external</ em > < code > hasOwnProperty</ code > in order to get correct results.</ p >
276
+ < pre > < code > var foo = {
277
+ hasOwnProperty: function() {
278
+ return false;
279
+ },
280
+ bar: 'Here be dragons'
281
+ };
282
+
283
+ foo.hasOwnProperty('bar'); // always returns false
284
+
285
+ // Use another hasOwnProperty and call it with 'this' set to foo
286
+ {}.hasOwnProperty.call(foo, 'bar'); // true
287
+ </ code > </ pre >
272
288
</ section > < section > < header > < h3 > In conclusion</ h3 > </ header >
273
289
< p > When checking for the existence of a property on a object, < code > hasOwnProperty</ code > is
274
290
the < strong > only</ strong > method of doing so. It is also recommended to make < code > hasOwnProperty</ code >
You can’t perform that action at this time.
0 commit comments