Questions tagged [hasownproperty]

hasOwnProperty is a method of the object object in JavaScript. It is used to check whether the calling object has the specified key as a direct member. It does not traverse the prototype chain.

hasOwnProperty is a method of most objects. A notable exception is the window object in IE.

116 questions
141
votes
6 answers

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

If I understand correctly, each and every object in JavaScript inherits from the Object prototype, which means that each and every object in JavaScript has access to the hasOwnProperty function through its prototype chain. While reading RequireJS'…
timkg
  • 1,773
  • 2
  • 14
  • 12
129
votes
11 answers

What is property in hasOwnProperty in JavaScript?

Consider: if (someVar.hasOwnProperty('someProperty') ) { // Do something(); } else { // Do somethingElse(); } What is the right use/explanation of hasOwnProperty('someProperty')? Why can't we simply use someVar.someProperty to check if an object…
FLY
  • 2,379
  • 5
  • 29
  • 40
28
votes
1 answer

object has no hasOwnProperty method (i.e. it's undefined) - IE8

This seems quite bizarre. Here's my experiment in the IE8 console: typeof obj1 // "object" obj1.hasOwnProperty // {...} typeof obj2 // "object" obj2.hasOwnProperty // undefined Any ideas as to what could cause this?
Gezim
  • 7,112
  • 10
  • 62
  • 98
26
votes
3 answers

Benefit of using Object.hasOwnProperty vs. testing if a property is undefined

Since hasOwnProperty has some caveats and quirks (window / extensive use in Internet Explorer 8 issues, etc.): Is there any reason to even use it? If simply testing if a property is undefined, is it better justified and more simplistic? For…
Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
25
votes
1 answer

Object.hasOwn() vs Object.prototype.hasOwnProperty()

The new method Object.hasOwn() returns a boolean indicating whether the specified object has the indicated property as its own property but so does Object.prototype.hasOwnProperty(), what is the difference between them and what is the benefit of…
20
votes
6 answers

JavaScript: Is a member defined?

It seems to me that there are four different ways I can determine whether a given object (e.g. foo) has a given property (e.g. bar) defined: if (foo.hasOwnProperty(bar)) { if ('bar' in foo) { if (typeof foo.bar !== 'undefined') { if (foo.bar ===…
user979672
  • 1,803
  • 3
  • 23
  • 32
16
votes
2 answers

check if object has key in Typescript and use that key

I have the following code: let show = { createTag: false, updateFeature: false, createFeatureGroup: false, deleteFeature: false, deleteCycle: false, }; And I'm getting a value from the querystring that I want to match agains the…
opensas
  • 60,462
  • 79
  • 252
  • 386
15
votes
2 answers

Why does hasOwnProperty not recognise functions on an object's prototype?

I understand that the hasOwnProperty method in JavaScript exists to identify properties only of the current type, but there is something in the prototype chain here that is confusing to me. Let us imagine that I define a type called Bob, and assign…
glenatron
  • 11,018
  • 13
  • 64
  • 112
13
votes
1 answer

Fixing "Do not access Object.prototype method 'hasOwnProperty' from target object" error

Based on hasOwnProperty() method docs I wrote the following: const myObj = { prop1: 'val1', prop2: 'val2' } if (!myObj.hasOwnProperty('prop3')) { myObj.prop3 = 'val3' } But I'm getting this error: Do not access Object.prototype method…
drake035
  • 3,955
  • 41
  • 119
  • 229
9
votes
0 answers

How to fix missing keys in Object.keys() compared to for...in with hasOwnProperty()

In some browsers, Object.keys() doesn't return all the keys that for-in loop with hasOwnProperty() returns. Is there a workaround without using for-in loops ? Also is there another object than window which exhibits the same bug or is it only a…
jlgrall
  • 1,652
  • 1
  • 13
  • 13
8
votes
5 answers

Why don't toString and hasOwnProperty (etc) show up in for-in loops in JavaScript?

I was talking about hasOwnProperty with another developer and how you are supposed to use it in for-in loops in javascript and he had a good question. When you do a for-in loop, why doesnt toString, hasOwnProperty, and other built in methods show…
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
8
votes
6 answers

hasOwnProperty with more than one property

I'm trying to discover if an object has some properties and I'm having trouble using the hasOwnProperty method. I'm using the method on an array (I know the documentation states a string). The following line returns true: { "a": 1, "b": 2…
Caio Gomes
  • 681
  • 1
  • 11
  • 23
6
votes
1 answer

Why can't "hasOwnProperty" be used on instanceof HTMLInputElement?

I want to check if an input element is a checkbox or text type. I know I can do this: //Type of input.. if ( input.type === "checkbox" ) //Contains the property.. if ( "checked" in input ) But my question is: why do hasOwnProperty returns…
6
votes
2 answers

Javascript hasOwnProperty always false on Event objects?

I was hoping somebody could help clarify the hasOwnProperty() method with relation to Event Objects. I am trying to clone a mouse event (eventually this object will be passed to an iframe) I have already built a 'clone' function - but whenever i…
kinderman
  • 63
  • 3
6
votes
1 answer

hasOwnProperty HTMLElement Firefox

Friends, I notice in Firefox v23.0.1 that, hasOwnProperty of HTMLElement(input,button..etc) doesn't work, button1.hasOwnProperty('id') = false I use for in to check: var str1 = ''; for (pp in button1) { if…
Lindy
  • 287
  • 5
  • 13
1
2 3 4 5 6 7 8