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
0
votes
3 answers

how to rename javascript object function hasOwnProperty?

hasOwnProperty is long and makes my code unreadable with long chained if statements. Is there a way to rename hasOwnProperty to 'hop', 'has' or just 'h' so i can say something like if(req.body.h('first_name') && req.body.h('last_name')) { …
user1709076
  • 2,538
  • 9
  • 38
  • 59
0
votes
1 answer

Object.hasOwnProperty multiple levels without error

I wonder if there is some way to use hasOwnProperty for an object for multiple levels. To illustrate: I have following object: var Client = { ID: 1, Details: { Title: 'Dr', Sex: 'male' } } I can now do the following in…
DavidDunham
  • 1,245
  • 1
  • 22
  • 44
0
votes
3 answers

Can I use a for loop with if statement in place of .hasOwnProperty()

I'm working through the freeCodeCamp javascript and got stuck on the "profile lookup" exercise because I forgot about the .hasOwnProperty() function, but I am still not sure why my original function did not work. I'm leaving in a portion of the…
0
votes
1 answer

Why does hasOwnProperty('toString') not work on an object?

I recently watched a guide and wanted better understanding about the concept of hasOwnProperty. According to Mozilla: "The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as own (not inherited)…
Twisted
  • 17
  • 1
  • 6
0
votes
1 answer

Using hasOwnProperty dynamically for error 'Cannot read property '...' of undefined'

I am getting the error Cannot read property 'billingDate' of undefined installment here is undefined response.detailsResponse.installment.billingDate I want to use hasOwnProperty but in a dynamic way, like I pass the path and the object (to…
S..
  • 101
  • 6
0
votes
1 answer

How to differentiate between property and value passed in a function together

I need to check if objects in an array to see if they include a property, and if so, whether the properties' values match. The property and value are given together in the function call: whatIsInAName( [ { first: "Romeo", last:…
tamir
  • 45
  • 4
0
votes
2 answers

Iterating through inherited object properties when hasOwnProperty() is false

I've been testing JavaScript's Object.prototype.hasOwnProperty. From my understanding, it is designed to weed out direct object properties from inherited object properties. However, in the contrived examples I've tested so far (including MDN's…
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
0
votes
2 answers

Weird .hasOwnProperty behaviour

In an effort to properly instantiate Typescript objects from data received over HTTP as JSON, I was exploring the possibility of using the for..in loop coupled with .hasOwnProperty() like so: class User { private name: string; private age:…
tiansivive
  • 506
  • 1
  • 4
  • 16
0
votes
6 answers

hasOwnProperty - prototype - doesn't work

I am trying to exclude the property c if found so it won't be added to the properties array, however, it is being added still. Why? var letters = function () { this.a = 5; this.b = 20; }; letters.prototype = { c: 10 }; var letters = new…
learningcoding
  • 187
  • 2
  • 14
0
votes
2 answers

hasOwnProperty -wont show property even if it has the property

I am trying to understand why the result when I call the below function is "no" because the property c should exist. Does anyone know why? Thanks!!! var letters = function() { this.a = 5; this.b = 20; }; letters.prototype = { c: 10 …
learningcoding
  • 187
  • 2
  • 14
0
votes
2 answers

Using function to for-in loop through object to change values then return result

I'm building a function that uses a for-in loop to go through any object, change it's values (whether they be a number, string or boolean) to a single specific string, then output it. I've been through a bunch of existing questions which have helped…
Kyle
  • 5
  • 4
0
votes
2 answers

Battle: hasOwnProperty vs obj.prop

I have seen some posts dedicated to hasOwnProperty but I still question whether (and when) it should be used over a simple obj.prop if (obj.hasOwnProperty("prop")) is useful for checking whether an object defines a non-inherited property prop but is…
dipole_moment
  • 5,266
  • 4
  • 39
  • 55
0
votes
2 answers

angularjs select showing non filtered selected

I'm ran across the following that I found to be strange. I'm not blocked by it but was curious if someone knew. When I use hasOwnProperty with a select option, it shows a value (A2F0C7) not in the dropdown as selected.. Can anyone share why this…
Daniel Cohen
  • 538
  • 1
  • 4
  • 6
0
votes
1 answer

Object - checking if field exists

I have an object with following markup : Object {workout: Object} workout: Object 1/12/2015: Array[3] 0: "workoutTitle0" 1: "workoutTitle1" 2: "workoutTitle2" 2/12/2015: Array[3] 3/12/2015: Array[3] 4/12/2015:…
marcinf2
  • 83
  • 11
0
votes
1 answer

Getting OOP function parameters to work properly

I have a simple OOP code I started: (function(window,document){ var _data = { get:function(d){ return _data.data[d] }, set:function(prop,param){ _data.data[prop]=param }, remove:function(d){ delete _data.data[d] }, …
EasyBB
  • 6,176
  • 9
  • 47
  • 77