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
1
vote
3 answers

Which is the best way to check if an object has property

I want to ask which is the right way to check if an ajax response object has a property . I googled and what I found is that there are different ways to approach this. For example: if(ajaxResponse.hasOwnProperty('someProperty')){ //do…
1
vote
1 answer

There is a bug in this object extender using hasOwnProperty, I'm uncertain what that bug is or where this extender

The following code is supposed to extend foo with bar, the assignment was to find the "bug" in this snippet but for the life of my I can't seem to find the bug. Is there something I'm missing? Some major case where this code would break when…
timewaster51
  • 120
  • 5
1
vote
2 answers

why property defined after object's instantiation is not an own property

Here i created an instance of parent class and defined a property called smile on the object itself. I know that the property defined on constructor's prototype is not the object's own property .But how come "smile" property have not passed the…
AL-zami
  • 8,902
  • 15
  • 71
  • 130
1
vote
5 answers

JavaScript: Why does the Object.hasOwnProperty method behave like this?

My understanding is that the Object.hasOwnProperty method checks if the object has a property name of it's own, meaning a non-inherited property. That would mean that the function should return false whenever the property a. doesn't exist, or b. if…
shmuli
  • 5,086
  • 4
  • 32
  • 64
1
vote
2 answers

is there a method equal to hasOwnProperty() that can traverse prototype chain?

for example i have an array , lets call it myArray where : var myArray = ['foo', 'bar']; even though , myArray.join() will return 'foo,bar' , the check myArray.hasOwnProperty('join') , will return false , because simply hasOwnProperty() will not…
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
1
vote
1 answer

Is there a need to use hasOwnProperty with Object.keys?

if I iterate over an object, I need to check whether it is not a prototype object I loop over. (with hasOwnProperty) If I collect the keys with Object.keys, I always just get the "real keys" back. Is this correct?
Christian
  • 6,961
  • 10
  • 54
  • 82
1
vote
2 answers

How to find all objects with a false value within an array?

What is the correct way to get all objectkeys with a false value in an array like this in Javascript: [ { unknownkey1 : false }, { unknownkey2 : true }, { unknownkey3 : false }, { unknownkey4 : true }, { unknownkey5 : false }, {…
user2429266
  • 390
  • 1
  • 3
  • 19
1
vote
0 answers

ValidityState hasOwnProperty always returns false

I ran into some very weird behaviour in regards to .hasOwnProperty() and the ValidityState object. In both IE (11) and FF (26), calling .hasOwnProperty() on ValidityState always returns false, even though the property is definitely set. In Chrome…
Chris
  • 44
  • 7
1
vote
2 answers

Does if (obj.nonExistentProperty) trigger any browser errors?

I'm iterating through an array of simple (JSON-style) objects, some of which have a given property, and some that don't. If they have that property, it's safe to assume that it will never evaluate to a falsy value. var objs = [ {'a': 1, 'b': 2,…
M Miller
  • 5,364
  • 9
  • 43
  • 65
1
vote
2 answers

Find if an object tree has a given property

I am passing a set of options as an object: var options={ sortRules:[ {...}, // rule 1 {...}, // rule 2 // etc. ], filterRules:[ {...}, // rule 1 {...}, // rule 2 // etc. ], …
Christophe
  • 27,383
  • 28
  • 97
  • 140
1
vote
1 answer

jshint no error for forin (hasOwnProperty)

Why jshint is not reporting forin (hasOwnProperty) error for the following code? jslint do report error on it but jshint doesn't. /*jshint forin: true */ (function () { "use strict"; var obj = {a: 1, b: 2}, i = null; for (i in obj) { …
iFadey
  • 320
  • 3
  • 11
1
vote
3 answers

How to iterate over a string and find out if it is available in an object literal?

var leet = { h: 1, e: 2, r: 3, o: 4, l: 5 }; var s = "hello"; var fin = ""; for (var i in s) { if (leet.hasOwnProperty(i)) { fin + = leet[i]; } else { fin + = i } } console.log(fin); Why am I…
user1223844
  • 87
  • 1
  • 2
  • 7
1
vote
1 answer

Is 'clearing' an object a viable way to avoid using hasOwnProperty?

I understand why hasOwnProperty is necessary even when one has complete control over an object, since Object.prototype may have been modified. But if I really want to avoid it, consider this: function CleanObject() { var result = {}; for…
DNS
  • 37,249
  • 18
  • 95
  • 132
1
vote
2 answers

Is a simple `continue` statement an acceptable alternative to nesting an entire for..in loop's body in the `if`?

Typically, the solution to for..in's notorious caveat is something along the lines of: for(var prop in obj) { if(obj.hasOwnProperty(prop)) { foo(); bar(); baz(); } } I feel like it would be cleaner to just do: for(var prop in obj)…
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
1
vote
2 answers

Javascript array. Advanced

I have JSON string from my php script like this: var r.co = { "A20018425":[ {"balance":"1390.31"}, // 1 {"balance":"1304.11"}, // 2 …
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60