简体   繁体   中英

Why hasOwnProperty returns false on a constructor function but string property returns true when accessed without instantiating the function

I basically understand, hasOwnProperty method in JavaScript is used to check whether the object has the specified property as its own property.

But the behaviour I checked in browser console below got me downright confused. Can someone explain me why this is happening. I did try finding existing answer, but couldnt find a relevant link.

在此处输入图片说明

If you check the typeof Shape it will return you a "function" but typeof s will return "object"

When you call var s = new Shape(); Shape will considered as class but name and size are members of Shabe class.

Default constructor will called on new Shape() and creates a object of Shape.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM