Javascript hasOwnProperty在Event对象上总是为false? [英] Javascript hasOwnProperty always false on Event objects?

查看:154
本文介绍了Javascript hasOwnProperty在Event对象上总是为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助澄清与事件对象有关的hasOwnProperty()方法。

I was hoping somebody could help clarify the hasOwnProperty() method with relation to Event Objects.

我正在尝试克隆一个鼠标事件(最终这个对象将是传递给iframe)
我已经构建了一个'clone'函数 - 但每当我尝试克隆一个窗口事件(即滚动,点击等)时,'hasOwnProperty()'的所有实例都返回false。
例如,我迭代对象 - 使用hasOwnProperty()来检查 - 并且每个属性都返回false。
这适用于标准对象 - 但不适用于事件对象。

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. For example, i iterate over the object - using hasOwnProperty() to check - and each property is returning 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 :)

代码片段:

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);


推荐答案

您的假设是正确的 - e 参数是一个空心的新的MouseEvent 对象,它没有自己的属性,只有那些从原型链继承的对象 MouseEvent< -UIEvent< - 活动。这是继承图:

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. Here's the inheritance diagram:

这篇关于Javascript hasOwnProperty在Event对象上总是为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆