Javascript hasOwnProperty always false on Event objects?

I was hoping someone could help clarify the hasOwnProperty () method in relation to event objects.

I am trying to clone a mouse event (this object will eventually be passed to the iframe) I have already created a "clone" function, but whenever I try to clone a window event (eg scroll, click, etc.), all instances' hasOwnProperty () return false. For example, I iterate over an object - using hasOwnProperty () to check - and each property returns false. This works for standard objects, but not for event objects.

Is it because all properties of event objects are inherited? Or is there a problem with the code?

Any enlightenment would be appreciated :)

Snippet of code:

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

      

+3


source to share


2 answers


Your guess is correct - the argument e

is an empty object new MouseEvent

that has no properties of its own, only those inherited from the prototype chain MouseEvent<-UIEvent<-Event

. Here's the inheritance diagram:



enter image description here

+2


source


You created a named object newObject

, but you returned a named object newNode

that you never defined or added. Try changing your return statement to this:

return newObject;

      



I think this will give you an object with some properties that the event itself has.

0


source







All Articles