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
}
}
Nice educative post.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I agree with you too dear @ghazanfar.ali
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good reading
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I love your programming short tutorials. It helps us learn some basics in proper way.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I agree with you too dear @zakir.quetta.so 100% love and 100%vote
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you dear
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
very helpful post for programmers. Thank you sir. (Hope you are well. Stay safe.)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Sir you may know that i am new in steemit. so would you please help me to do good and earn a handsome amount ?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
چقدر به زبان انسان نزدیکتره جاوا اسکریپت
در سی شارپ ارث بری رو با : نشون میدن
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good one. thanks for your great educative post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
درود
احسنت بر شما
عالیه
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi..@ghasemkiani sir ..I need your help..plz sir follow me....I am your ragular upvote and commenter plz sir follow me
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Your blogs about JavaScript are really interesting and informative.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
this is very inportant.. and very informative...
i support you definitily...
thanks for sharing....
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
its so valuable post...
i appreciate so much..so technical over all..
best of luck
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Bisyar khub...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good programming and Nice technoloy...and good post over all...
I like so much...
best of luck
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
keep it up learn much
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks for shairing this post. great technology @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Excellent informative post.Thanks for sharing precious post.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for info&visit @steemrobot
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Also thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wonderful post. It really very inportant.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This nice post i like it thanks for sharing this technology news best of luck..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
it is very important
this work is very hard
thanks for sharing a valuable blog
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for sharing this post..I appreciate technology..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very useful,, your posts with javascript programming are helping me to learn more about javascript. . Excellent post,, thank You sir @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Educative
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@ghasemkiani Yet another valuable blog. Thanks for sharing the JavaScript tutorial. Keep sharing.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Well I am still a rookie in Java. You looks like an expert level.
You should try Unity :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
your are really good in java programming
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very helpful for learning programming.This serice tutorial is so helpful for a student.
Thanks @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks for this post to @rasel1234
you aer giving a good post,,
I love your post,,
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great post ghasemkiani👌👌👌i like your all post dear...
it is an important post i think...
please carry on your activity...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good post dear @ghasemkiani carry on......
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I love this article,thanks for the information
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
That's really cool information @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Important post for me. JavaScript is very important for web developing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
really a good educative post :) thanx for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
JavaScript is very important for web developing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
well information i like it ............... keep it up thanks for shareing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
that's pretty cool to know....
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
love to read it .........
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
fabulous one..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This is so great! You got the great point here......
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
love to read your post.
thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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!!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I really like this post ghasemkiani!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice informative post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit