Javascript 如何从jQuery继承

Javascript 如何从jQuery继承,javascript,jquery,inheritance,Javascript,Jquery,Inheritance,我尝试以以下方式继承: function inherits(ctor, base_ctor){ ctor.base = base_ctor; ctor.prototype = Object.create(base_ctor.prototype, { constructor: { value: ctor, enumerable: false, writable: true,

我尝试以以下方式继承:

function inherits(ctor, base_ctor){
    ctor.base = base_ctor;
    ctor.prototype = Object.create(base_ctor.prototype, {
        constructor: {
            value: ctor,
            enumerable: false,
            writable: true,
            configurable: true,
        }
    });
}
function MyClass(){
    jQuery.fn.init.call(this,'<form>...</form>');
    //...
}
inherits(MyClass, jQuery);
//...
var $my_obj = new MyClass()
它再次调用MyClass的构造函数(通过函数jQuery.fn.pushStack())

如何避免递归?

或者如何从jQuery继承(而不是扩展?

为此,jQuery提供函数,然后您可以通过任何方法扩展jQuery的副本,但[FIXME]您不能使用自己的构造函数

此外,您还可以使用以下黑客:

function MyClass(){
    if(this.property_that_can_not_be_created_by_jQuery)
        return jQuery.apply(this,arguments); // do not use jQuery.fn.init
    this.property_that_can_not_be_created_by_jQuery = true;

    jQuery.fn.init.call(this,'<form>...</form>');
    //...
}
inherits(MyClass, jQuery);
函数MyClass(){
if(此.property\u不能\u由\u jQuery创建\u)
返回jQuery.apply(this,arguments);//不要使用jQuery.fn.init
this.property_that_can_not_created_by_jQuery=true;
jQuery.fn.init.call(这个“…”);
//...
}
继承(MyClass,jQuery);

但请注意,pushStack()和任何使用pushStack()的函数(例如appendTo()和insertAfter())都将返回由jQuery的cunstructor创建的新对象,如果要获取由构造函数构造的对象,应该使用方法

,为什么要从jQuery继承?没有必要这样做。如果你需要回美元符号,请使用。看一看。但是我认为这是个坏主意。
function MyClass(){
    if(this.property_that_can_not_be_created_by_jQuery)
        return jQuery.apply(this,arguments); // do not use jQuery.fn.init
    this.property_that_can_not_be_created_by_jQuery = true;

    jQuery.fn.init.call(this,'<form>...</form>');
    //...
}
inherits(MyClass, jQuery);