JavaScript Basics: Object.prototype.hasOwnProperty

in javascript •  7 years ago  (edited)

Objects in JavaScript have not only their own properties, but also the properties that are on the objects in their prototype chain. Object.keys gives a collection of the object's own properties, but the for...in loop enumerates all properties of the object, including its own properties and its inherited properties.

How can you determine if a particular property is an own property or an inherited property? For this, you can use Object.prototype.hasOwnProperty. Since this method is available on Object.prototype, it is accessible to any object that descends from Object.prototype.

Here is an example:

    let myObj = {
        name: "Ali",
    };
    myObj.hasOwnProperty("name"); // true
    myObj.hasOwnProperty("toString"); // false

You may have noticed that while this method is on Object.prototype, most of the other functions for object manipulation are on the Object constructor itself. This makes it handy, because the method can be called directly on the object, but there is also a problem, and it is the fact that the object may not have descended from Object.prototype. As I wrote in a previous post, In JavaScript, an object may have no prototype. You can call Object.setPrototypeOf(myObj, null);. Then, if you call myObj.hasOwnProperty("name"), you will receive an error:

    TypeError: myObj.hasOwnProperty is not a function

For this reason, it is recommended that you call this function like this:

    Object.prototype.hasOwnProperty.call(myObj, "name");

Of course, if you know your object's inheritance, you don't need to follow this precaution.

A typical use case for Object.prototype.hasOwnProperty is in for...in loops. Since for...in loops over all the properties of the object, whether own or inherited, sometimes it is necessary to determine whether the property is an own property. Here is an example:

    for(let prop in myObj) {
        if(myObj.hasOwnProperty(prop)) {
            // do something with prop
        }
    }

Related Posts

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Nice educative post.

I agree with you too dear @ghazanfar.ali

good reading

I love your programming short tutorials. It helps us learn some basics in proper way.

I agree with you too dear @zakir.quetta.so 100% love and 100%vote

Thank you dear

very helpful post for programmers. Thank you sir. (Hope you are well. Stay safe.)

Sir you may know that i am new in steemit. so would you please help me to do good and earn a handsome amount ?

چقدر به زبان انسان نزدیکتره جاوا اسکریپت
در سی شارپ ارث بری رو با : نشون میدن

ممنون. دستور نگارش (syntax) جاوا اسکریپت مانند زبان جاوا است.

good one. thanks for your great educative post

درود
احسنت بر شما
عالیه

Hi..@ghasemkiani sir ..I need your help..plz sir follow me....I am your ragular upvote and commenter plz sir follow me

Your blogs about JavaScript are really interesting and informative.

  ·  7 years ago Reveal Comment

this is very inportant.. and very informative...
i support you definitily...
thanks for sharing....

its so valuable post...
i appreciate so much..so technical over all..
best of luck

Bisyar khub...

Nice educative post dear @ghasemkiani and great writing. i like and support your post all time. dear @ghasemkiani i will naver forget you.

keep it up dear
@ghasemkiani

good programming and Nice technoloy...and good post over all...
I like so much...
best of luck

keep it up learn much

thanks for shairing this post. great technology @ghasemkiani

Excellent informative post.Thanks for sharing precious post. Screenshot_20180121-165547.png

Thanks for info&visit @steemrobot

Also thanks

Wonderful post. It really very inportant.

This nice post i like it thanks for sharing this technology news best of luck..

it is very important
this work is very hard
thanks for sharing a valuable blog

Thanks for sharing this post..I appreciate technology..

Dear sir...hopefuly u r good..l
Your bolg alwayas educational for us..which are really helpful for me..Javescript is very much important for konwing this course..
thank you sir for ur konwledge sharing us..

Very useful,, your posts with javascript programming are helping me to learn more about javascript. . Excellent post,, thank You sir @ghasemkiani

Educative

@ghasemkiani Yet another valuable blog. Thanks for sharing the JavaScript tutorial. Keep sharing.
thanks you.gif

Well I am still a rookie in Java. You looks like an expert level.
You should try Unity :)

Thanks for your valuable and informative post about science & technology.
We can gather a lot of information by your post.
By dint of, we can increase our skill that is beneficial for all steemians.
I will always visit your site & wait for your upcoming post.
Thanks

your are really good in java programming

Very helpful for learning programming.This serice tutorial is so helpful for a student.
Thanks @ghasemkiani

thanks for this post to @rasel1234
you aer giving a good post,,
I love your post,,

great post ghasemkiani👌👌👌i like your all post dear...
it is an important post i think...
please carry on your activity...

good post dear @ghasemkiani carry on......

I had a good idea about the Java script but. I did not want to practice, I forgot my mother.♦thanks for sharing about technology.......@ghasemkiani

I love this article,thanks for the information

thank you @ghasemkiani for this post
Dear friend
steam world you have successfully opened the edge. And you have successfully top level reached, this is my wish. And I am glad this is my practice in your development.
My request to you, I am like a small child in the steam world unknown and withut benefited.
You must know
"People for people"
So went my way in the world, steam your collaboration service.
Please do follow me and help and thanks to me

  ·  7 years ago Reveal Comment

That's really cool information @ghasemkiani

Important post for me. JavaScript is very important for web developing

Very useful post about JavaScript, In Future it will Move more forward with this kind of update technology. Thank you for share with us. @ghasemkiani

really a good educative post :) thanx for sharing

JavaScript is very important for web developing

well information i like it ............... keep it up thanks for shareing

that's pretty cool to know....

love to read it .........

fabulous one..

This is so great! You got the great point here......

love to read your post.
thanks for sharing

Hello dear STEEMIAN... You received this message as a follower of @lightingmacsteem.

Do support the JOULESTEEM circuit posts to be able to learn ways to harness offgrid energy the non-wind nonsolar way. One-third of all author SBD rewards will also be distributed to all upvoters and resteemers.

Visit the posts of @minnowminer and @lightingmacsteem to support, earn and learn!!!

I really like this post ghasemkiani!

nice informative post

Your post is very nice ... @ghasemkiani
I upvote 100% of your posts ...
Continue to work ...

If you do not mind my upvote back posting too: https://steemit.com/crypto/@aaabangganteng/my-problem-with-bitcoin-usdbtc-today-vs-nasdaq-99-bubble-chart-in-3-pictures

great post fully informative one should want to learn about #javascript these tips are real gold for them . amazing dude . Thank you @ghasemkiani for this valuable share