Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript类型错误,不是函数_Javascript_Jquery - Fatal编程技术网

Javascript类型错误,不是函数

Javascript类型错误,不是函数,javascript,jquery,Javascript,Jquery,我有一个奇怪的问题,我似乎解决不了!这是我正在编写的一个大框架的一部分,但我已经编写了一些测试代码,它们也有同样的问题。见下文: !function ($, window, undefined) { // BASE FUNCTION var test = function (selector, context) { return new test.fn.init(selector, context); }; // SELECTOR FUNCTI

我有一个奇怪的问题,我似乎解决不了!这是我正在编写的一个大框架的一部分,但我已经编写了一些测试代码,它们也有同样的问题。见下文:

!function ($, window, undefined) {

    // BASE FUNCTION
    var test = function (selector, context) {
        return new test.fn.init(selector, context);
    };

    // SELECTOR FUNCTIONS
    test.fn = {
        selector:   undefined,
        init:       function (selector, context) {
            // Use jQuery to build selector object
            this.selector = $(selector, context);
            return this;
        },

        // Create a popup dialog
        popup:      function (options) {
            this.selector.dialog();
        }
    },

    // Expose Carbon to the global object
    window.test     = test;

}(window.jQuery, window);
test('#popupLink').popup();
现在,当我使用以下命令时:

!function ($, window, undefined) {

    // BASE FUNCTION
    var test = function (selector, context) {
        return new test.fn.init(selector, context);
    };

    // SELECTOR FUNCTIONS
    test.fn = {
        selector:   undefined,
        init:       function (selector, context) {
            // Use jQuery to build selector object
            this.selector = $(selector, context);
            return this;
        },

        // Create a popup dialog
        popup:      function (options) {
            this.selector.dialog();
        }
    },

    // Expose Carbon to the global object
    window.test     = test;

}(window.jQuery, window);
test('#popupLink').popup();
我得到“TypeError:test(#populink”)。popup不是一个函数。我知道这在一定程度上是可行的,因为如果我执行以下操作,我可以使用标准jQuery函数:

test('#popupLink').selector.hide();
任何帮助都将不胜感激,因为我现在有精神障碍。 提前感谢!:)

更新

我使用console.log来查看返回的对象,它在中只有selector元素,这很有意义,因为我没有使用prototype。我怎样才能解决这个问题

(function ($, window) {
    // BASE FUNCTION
    var test = function (selector, context) {
        return new test.fn.init(selector, context);
    };

    // SELECTOR FUNCTIONS
    test.fn = test.prototype = {
        constructor: test,
        init: function (selector, context) {
            // Use jQuery to build selector object
            this.selector = $(selector, context);
            return this;
        },

        // Create a popup dialog
        popup: function (options) {
            console.log('popup');
            return this;
        }
    };

    // Expose test to the global object
    window.test = test;
}(window.jQuery, window));

test.fn.init.prototype = test.fn;

您错过了创建的测试实例上的构造函数和原型链。

在我的例子中,错误是在添加集成测试时发生的,我没有注意到我尝试调用的函数属于模拟类。很容易混淆,因为当尝试导航(我使用IntelliJ)时,IDE会转到实现的函数


模拟错误中提到的函数解决了这一问题。

将test.fn更改为test.prototype或test.fn.prototype我添加了“test.fn.prototype=test.fn”,但仍然不起作用,完全相同的错误:(你必须将弹出窗口指定给test.fn.init.Ah的原型,我现在明白了。非常感谢!实际上还有一件事,它有什么作用“constructor:test”在这种情况下可以做什么?实际上,nevermind解决了这个问题。如果你做“new test()”,它会使用原始的测试函数作为构造函数。d'oh!返回一个对创建实例原型的函数的引用。将它从一个对象更改为另一个测试,如果你不使用它,你可以删除它。