Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Type Error, is not a function

I've got abit of a strange problem, that I just can't seem to solve! It's part of a big framework I'm writing, but I've wrote some test code which has the same problem. See the following:

!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);

Now when I use the following:

test('#popupLink').popup();

I get "TypeError: test("#popupLink").popup is not a function". I know it's partly working, as I can do use standard jQuery functions if I do something like:

test('#popupLink').selector.hide();

Any help would be greatly appreciated, as I'm having a mental block right now. Thanks in advance! :)

Update

I've used console.log to view the returned object, and it only has the selector element in, which makes sense as I didn't use prototype. How can I fix that?

like image 553
jleck Avatar asked Jun 19 '12 21:06

jleck


People also ask

Is not a function type error JavaScript?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How do I fix TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

What is a type error JavaScript?

A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or. when attempting to modify a value that cannot be changed; or. when attempting to use a value in an inappropriate way.

Is function a type in JavaScript?

Introduction to the JavaScript Function type In JavaScript, all functions are objects. They are the instances of the Function type. Because functions are objects, they have properties and methods like other objects.


1 Answers

(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;

You missed the constructor and the prototype chain on the created instances of test.

like image 123
jasssonpet Avatar answered Sep 28 '22 03:09

jasssonpet