简体   繁体   English

用ES6扩展jQuery

[英]Extend jquery with ES6

I hacked away at this way to extend jQuery as an ES6 class. 我以这种方式砍掉了将jQuery扩展为ES6类的方法。 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. 它可以在0.5秒内创建其自身的1000个新实例。 This is to be used at MOST for generating table rows that are objects (for complex, updating tables that do not empty themselves). 这将在MOST处用于生成作为对象的表行(对于复杂的,更新的表,这些表不会自己清空)。 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__) 我想知道是否有比此更好的方法来继承jQuery原型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/ https://jsfiddle.net/ctyzaphw/3/

Thats an interesting experiment. 多数民众赞成在一个有趣的实验。 But you might want to look into Object.assign from es15. 但是您可能想研究es15中的Object.assign。 Full description is available here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign 此处提供完整描述: 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) 通过获取目标和源Object.assign(target,source)工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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