为什么“ getOwnPropertyDescriptor”返回一个值,而“ hasOwnProperty”却返回“ false”?

时间:2020-04-07 12:09:22

标签: javascript

Number是一个内在函数对象,它是globalThis上的函数属性。

console.log(Object.getOwnPropertyDescriptor(globalThis, 'Number')) // <descriptor>

那么,globalThis.hasOwnProperty(window, 'Number')为什么返回false

console.log(globalThis.hasOwnProperty(window, 'Number')) // false

我确定我缺少明显的东西...

2 个答案:

答案 0 :(得分:2)

错误在于您对hasOwnProperty的调用,它是成员函数而不是类函数。因此,您的呼叫应为hasOwnPropery('Number'),现在为hasOwnPropery(window, 'Number'),因为globalThis没有window属性,以后的所有参数都将被忽略。

const globalThis = window;
console.log(Object.getOwnPropertyDescriptor(globalThis, 'Number'))
console.log(globalThis.hasOwnProperty('Number'))

答案 1 :(得分:1)

来自docs

pyspark.sql.utils.AnalysisException: "cannot resolve 'array(`id`, `model_score`, `tx_amount`, `isValid`, `greeting`)' due to data type mismatch: input to function array should all be the same type, but it's [int, double, double, boolean, string] 方法返回一个布尔值,指示是否 对象具有指定的属性作为其自己的属性(相对于 继承)。

语法是:

obj.hasOwnProperty(prop)

因此,如果要检查:hasOwnProperty()是否是Number的财产,可以尝试:

window

您不需要提供第二个参数。也许您误解了使用getOwnPropertyDescriptorconsole.log(window.hasOwnProperty('Number')) // true // or console.log(globalThis.hasOwnProperty('Number')) // true的方式

相关问题