简体   繁体   中英

Extend jquery with ES6

I hacked away at this way to extend jQuery as an ES6 class. It seems to accomplish everything, but I am concerned about speed, so I also ran a test. It can create 1000 new instances of itself in under 0.5 seconds. This is to be used at MOST for generating table rows that are objects (for complex, updating tables that do not empty themselves). I am wondering if there is a better way to inherit the jquery prototypes than this this.__proto__ = $.extend(true, this.__proto__, this.__proto__.__proto__)

class Popup extends jQuery.fn.init {
  constructor() {
    super('<div>test</div>');
    this.$wrapper = null;

    this.__proto__ = $.extend(true, this.__proto__, this.__proto__.__proto__)
    return this;
  }

  test() {
    console.log('hi')
  }
}

https://jsfiddle.net/ctyzaphw/3/

Thats an interesting experiment. But you might want to look into Object.assign from es15. Full description is available here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Works by taking a target and a source Object.assign(target, source)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM