简体   繁体   English

为什么 hasOwnProperty 在构造函数上返回 false,但在未实例化函数的情况下访问字符串属性时返回 true

[英]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.我基本明白了,JavaScript 中的hasOwnProperty方法是用来检查对象是否有指定的属性作为自己的属性。

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"如果您检查 typeof Shape 它将返回一个“函数”但 typeof s 将返回“对象”

When you call var s = new Shape();当你调用 var s = new Shape(); Shape will considered as class but name and size are members of Shabe class. Shape 将被视为类,但名称和大小是 Shabe 类的成员。

Default constructor will called on new Shape() and creates a object of Shape.默认构造函数将调用 new Shape() 并创建一个 Shape 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 performance.hasOwnProperty('getEntries') 返回 false 而 typeof performance.getEntries 返回 function? - Why performance.hasOwnProperty('getEntries') returns false while typeof performance.getEntries returns function? javascript hasOwnProperty返回true而不是false? - javascript hasOwnProperty returns true instead of false? hasOwnProperty在FF中返回false - hasOwnProperty returns false in FF 创建一个基于输入返回true,false或字符串的函数 - Create a function that returns true, false or a string based on the input Function 返回 false 但我需要为什么返回 true 反应原生 - Function returns false but i need why return true react native 当函数在Javascript中将布尔值设置为true时,布尔值将返回false - Bool value returns false when the function set it to true in Javascript 在对象数组中搜索对象时,是否有返回 true 或 false 的函数? - Is there a function which returns true or false when searching for an object in an array of objects? 当Javascript函数返回false时返回true - Javascript function returns true when it should return false 如果if语句中的JavaScript函数为true,则它返回false。 - Javascript function returns false when it should be true from an if statement? 字符串返回真/假 - String Returns True/False
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM