[英]Javascript hasOwnProperty always false on Event objects?
I was hoping somebody could help clarify the hasOwnProperty() method with relation to Event Objects. 我希望有人可以帮助澄清与事件对象有关的hasOwnProperty()方法。
I am trying to clone a mouse event (eventually this object will be passed to an iframe) I have already built a 'clone' function - but whenever i attempt to clone a window event (ie scroll, click etc) all instances of 'hasOwnProperty()' return false. 我正在尝试克隆一个鼠标事件(最终这个对象将传递给iframe)我已经构建了一个'克隆'函数 - 但每当我尝试克隆一个窗口事件(即滚动,点击等)时,'hasOwnProperty的所有实例()'返回false。 For example, i iterate over the object - using hasOwnProperty() to check - and each property is returning false. 例如,我迭代对象 - 使用hasOwnProperty()来检查 - 并且每个属性都返回false。 This works for standard objects - but not event objects. 这适用于标准对象 - 但不适用于事件对象。
Is this because all of the properties within the event objects are inherited? 这是因为事件对象中的所有属性都是继承的吗? Or is there an issue with the code? 或者代码有问题吗?
Any enlightenment would be appreciated :) 任何启示将不胜感激:)
Code snippet: 代码段:
function cloneObject (o_node) {
var newObject = {};
for (var child_node in o_node) {
if (o_node.hasOwnProperty(child_node)) {
//no object properties are returning true at this point.
newObject[child_node] = o_node[child_node];
}else{
console.log("!hasOwnProperty()");
}
}
return newNode;
}
function onclick(e){
var cloned_object_e = cloneObject(e); //returns an empty object;
}
window.addEventListener('click', onclick);
Your assumption is correct - the e
argument is a hollow new MouseEvent
object that has no own properties, only those inherited from the prototype chain MouseEvent<-UIEvent<-Event
. 您的假设是正确的 - e
参数是一个空洞的new MouseEvent
对象,它没有自己的属性,只有那些从原型链MouseEvent<-UIEvent<-Event
继承的对象。 Here's the inheritance diagram: 这是继承图:
You created an object called newObject
, but you returned an object called newNode
, which you never defined or added anything to. 您创建了一个名为newObject
的对象,但是您返回了一个名为newNode
的对象,您从未定义或添加任何内容。 Try changing your return statement to this: 尝试将return语句更改为:
return newObject;
I think this will give you an object with some properties that the event itself has. 我认为这将为您提供一个具有事件本身具有的属性的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.