Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript hasOwnProperty always false on Event objects?

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

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

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);
like image 693
kinderman Avatar asked Jul 31 '15 18:07

kinderman


1 Answers

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:

enter image description here

like image 112
georg Avatar answered Oct 10 '22 23:10

georg