我有一个点击处理程序
e.addEventListener('click', this.Multiply, false);
和一个函数
this.Multiply = function () {
APi.Multiply(this);
};
和简单的选择元素。
所以这得到了 Select 元素
为什么要运行
this.selectedIndex
给出值(value)=> 2
但是运行 this.getOwnPropertyNames()
会抛出错误
或 this.hasOwnProperty('selectedIndex')
-> 给出 false?
最佳答案
这是因为 selectedIndex 实际上是 HTMLSelectElement 原型(prototype)上的属性,而不是实例属性。要根据需要进行测试,您可以尝试类似的方法:
this.__proto__.hasOwnProperty('selectedIndex');
或
this.constructor.prototype.hasOwnProperty('selectedIndex');
您应该会得到预期的结果。
当然,假设您的 this
实例实际上是您选择的元素。当然给定这个 HTML:
<select id="example"></select>
运行这个javascript:
var el = document.getElementById("example");
console.log(el.__proto__.hasOwnProperty('selectedIndex'));
将在控制台打印 true。
关于javascript - 为什么 elem.hasOwnProperty() 是假的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32007854/