cloning javascript event object

前端 未结 4 1472
情歌与酒
情歌与酒 2020-11-28 13:07

Anybody know how to do a deep copy/cloning of a native javascript event object? I know I can create a new event object and set the appropriate properties manually to match

4条回答
  •  旧巷少年郎
    2020-11-28 13:36

    Inspired by Kofifus answer I just do that

    function cloneEvent(type, event) {
        var evt = new Event(type);
        return Object.setPrototypeOf(evt,event);
    }
    

    or just

    function cloneEvent(event){
      return Object.create(event)
    } 
    

    it returns a new object that protects you from edit the original object. but let you read and edit the property. thanks to js prototype chain

提交回复
热议问题