扩展 jQuery 在每个方法调用上调用构造函数
Extending jQuery calls constructor on each method call
我痛苦地试图将jquery扩展到ES6类中,然后用Babel进行转译。除了每次调用继承的jQuery函数之外,一切实际上都可以工作,jQuery似乎每次都调用构造函数。这在这部小提琴中很明显。此外,trigger
似乎被叫了 4 次。
(转译代码)https://jsfiddle.net/w3bbzgrn/1/
这是源代码
class Popup extends jQuery.fn.init {
constructor(t) {
super(t);
this.init(t);
return this;
}
test(){
this.trigger('test', 55)
return this
}
}
class Foo extends Popup{
constructor(t){
super(t)
console.log("I get called")
return this
}
}
var t = $('<div>hi</div>')
var f = new Foo(t);
f.on('test', function(e,args){
console.log(args)
})
f.appendTo('body').test().slideUp()
您可以使用
var stack = new Error().stack
转储当前调用堆栈,并查看构造函数被调用两次的原因。请参阅更新的 JSFiddle: https://jsfiddle.net/w3bbzgrn/3/
这将打印到控制台:
PRINTING CALL STACK
Error
at new Foo (VM149:90)
at window.onload (VM149:102)
I get called
PRINTING CALL STACK
Error
at new Foo (https://fiddle.jshell.net/_display/:90:15)
at Foo.pushStack (https://code.jquery.com/jquery-3.0.0.js:141:32)
at Foo.jQuery.fn.(anonymous function) [as appendTo] (https://code.jquery.com/jquery-3.0.0.js:5920:15)
at window.onload (https://fiddle.jshell.net/_display/:106:3)
I get called
55
因此,它被调用两次,但在两种不同的情况下:
- 第一次,它被称为因为
var f = new Foo(t);
- 第二次,当使用
f.appendTo('body')
.看起来它试图在附加元素之前创建元素的副本。
也许尝试谷歌搜索类似"jquery append 创建重复项"之类的东西。
相关文章:
- 使用Google Visualization动态调用构造函数
- 拦截对构造函数的调用
- 在javascript中对数字调用构造函数
- 我应该在原型上还是在新创建的实例上调用构造函数方法
- 在javascript中调用构造函数中函数的对象
- 从“”调用构造函数;摘要“;基本“;类”;
- jshint 和 Function() gets:调用构造函数时缺少“new”前缀
- 缺少调用构造函数的“()”
- JS中的自调用构造函数
- 如何在javascript中从另一个类调用构造函数
- 如何在不调用构造函数的情况下复制对象及其原型链?
- 在javascript中未调用构造函数时绑定此函数
- 如果构造函数在另一个函数中,则新创建的对象调用构造函数undefined
- Javascript调用构造函数中的构造函数
- 为什么我不能在我的模块中调用构造函数方法?
- 覆盖Ext.data.存储,调用构造函数
- 扩展 jQuery 在每个方法调用上调用构造函数
- 如何通过方法传递类实例而不调用构造函数
- 在子生成器中只调用构造函数
- Javascript语法[..]]在函数调用/构造函数调用前