Javascript what is a property in hasOwnProperty?

If( somevar.hasOwnProperty('someProperty') ) { // do something(); } else { // do sometingelse(); } 

What is the correct use / explanation of hasOwnProperty('someProperty') ?

Why can't we just use somevar.someProperty to check if the somevar object somevar property called someProperty ?

What is the property in this case?

What property does this javascript validate?

+29
javascript object hasownproperty
Feb 22 2018-12-12T00:
source share
7 answers

hasOwnProperty returns a boolean value indicating whether the object to which you call it has a property with the name of the argument. For example:

 var x = { y: 10 }; console.log(x.hasOwnProperty("y")); //true console.log(x.hasOwnProperty("z")); //false 

However, he does not look at the prototype chain of the object.

It is useful to use it when listing properties of an object with a for...in construct.

If you want to see the full details, the ES5 specification is , as always, a good place to view.

+57
Feb 22 2018-12-22T00:
source share

he checks:

Gets a boolean value indicating whether the object has a property with the specified name

The hasOwnProperty method returns true if the object has a property of the specified name, and false if it is not. This method does not check if a property exists in the object prototype chain; the property must be a member of the object itself.

Example:

 var s = new String("Sample"); document.write(s.hasOwnProperty("split")); //false document.write(String.prototype.hasOwnProperty("split")); //true 
+7
Feb 22 '12 at 14:20
source share

Here is a short and accurate answer:

In javascript, each object has a bunch of built-in key-value pairs that have meta-information about the object. When you loop all key-value pairs with for...in construct / loop for an object that you also scroll through these meta-information key-value pairs (which you definitely don't need).

enter image description here

Using hasOwnPropery(property) filter-out , this unnecessary cycle through meta-information directly checks whether the property parameter is a user-specified object in the object. By filters, I mean that hasOwnProperty(property) does not look if property exists in the prototype chain of the object, as well as meta information.

It returns boolean true/false based on this.

Here is an example:

 var fruitObject = {"name": "Apple", "shape": "round", "taste": "sweet"}; console.log(fruitObject.hasOwnProperty("name")); //true console.log(Object.prototype.hasOwnProperty("toString");) //true because in above snapshot you can see, that there is a function toString in meta-information 

Hope this is clear!

+2
Sep 26 '17 at 11:28
source share

hasOwnProperty is a normal Javascript function that takes a string argument.

In your case somevar.hasOwnProperty ('someProperty') it checks that somevar function has somepropery or not, it returns true and false

Tell me

 function somevar() { this.someProperty= "Generic"; } function welcomeMessage() { var somevar1= new somevar(); if(somevar1.hasOwnProperty("name")) { alert(somevar1.hasOwnProperty("name"));// it will return true } } 
+1
Feb 22 2018-12-12T00:
source share

You use object.hasOwnProperty ( p ) to determine if an object has an enumerated p property -

an object can have its own prototype, where default methods and attributes are assigned to each instance of the object. hasOwnProperty returns true only for properties that were specially set in the constructor or added to the instance later.

to determine if p is defined at all, anywhere, for an object, use if ( p is an instance of the object), where p evaluates the property name string.

For example, by default, all objects have a 'toString' method, but it will not be displayed in hasOwnProperty.

+1
Feb 22 '12 at 14:45
source share

checks if an object has a property . As far as I know, it works the same as if(obj.prop) .

0
Feb 22 2018-12-22T00:
source share

I also wrote this simple object-hasOwnProperty reusable component that returns a boolean to check whether the object has the specified property. It has 2 examples and 3 tests that can help to better understand the real use behavior.

Examples:

 hasOwnProperty({foo: 'bar'}, 'foo') // => true hasOwnProperty({foo: 'bar'}, 'bar') // => false 

Proven Use Cases:

 hasOwnProperty() should return false as given object do not contain key `bar` hasOwnProperty() should return true as given object contains key `foo` hasOwnProperty() should return false if key is null 

Here's how it works:

 function hasOwnProperty(obj: {}, prop: string|number): boolean { return Object.prototype.hasOwnProperty.call(obj, prop); }; 
0
May 28 '17 at 13:10
source share



All Articles