最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery : undefined is not a function - Stack Overflow

programmeradmin5浏览0评论

I have a code below and when i try to run get the error when i call the function . I wonder hey this his happening ? help plz ...

jQuery(document).ready(function($) {
        //load config file 
        $.getScript(baseURl+"/wsj/wconf.js", function(data, textStatus, jqxhr) {
            console.log(data); // it is ok 

                jq();  // Uncaught TypeError: undefined is not a function 
                //_cusApp.ini();  //Uncaught TypeError: Cannot call method 'ini' of undefined 

                var _cusApp = {
                        ini: function (inc) {
                            console.log('ini'); 
                        },
                };

                var jq = function ( ){
                    $(document).height(); // jquery not availble here   
                }
        }); 

    });

I have a code below and when i try to run get the error when i call the function . I wonder hey this his happening ? help plz ...

jQuery(document).ready(function($) {
        //load config file 
        $.getScript(baseURl+"/wsj/wconf.js", function(data, textStatus, jqxhr) {
            console.log(data); // it is ok 

                jq();  // Uncaught TypeError: undefined is not a function 
                //_cusApp.ini();  //Uncaught TypeError: Cannot call method 'ini' of undefined 

                var _cusApp = {
                        ini: function (inc) {
                            console.log('ini'); 
                        },
                };

                var jq = function ( ){
                    $(document).height(); // jquery not availble here   
                }
        }); 

    });
Share Improve this question edited Aug 9, 2013 at 11:39 putvande 15.2k3 gold badges36 silver badges51 bronze badges asked Aug 9, 2013 at 11:23 Pradeep JaiswarPradeep Jaiswar 1,8057 gold badges28 silver badges48 bronze badges 1
  • I think there are confliction with other jQuery. Please check this – Prashant Parekh Commented Aug 9, 2013 at 11:25
Add a ment  | 

3 Answers 3

Reset to default 6

It's about invoking jq() function before it's declared.

jq is undefined, because it's not declared yet...!

Update (@David Barker suggestion)

The whole code would work if a named function would be declared instead of an anonymous one.

As an anonymous function is created during run-time, it's not available until the assignment is executed.

In opposite, as a named function is declared in parse time, it's available to the code even if it's declared after invoking it.

Example of anonymous function

myFunction(); // Wrong, it's undefined!

var myFunction = function() {};

myFunction(); // OK, now is declared and it can be invoked

Example of named function

myFunction(); // As the function has been already parsed, it is available!

function myFunction() {};

myFunction(); // OK!

it should be

jQuery(function($) {
    //load config file 
    $.getScript(baseURl+"/wsj/wconf.js", function(data, textStatus, jqxhr) {
        console.log(data); // it is ok 

        //these two variable declaration shoule happen before their usage
        var _cusApp = {
            ini: function (inc) {
                console.log('ini'); 
            },
        };

        var jq = function ( ){
            $(document).height(); // jquery not availble here   
        }

        jq();
        _cusApp.ini();
    }); 

});

First you declare a function and then call her because javascript read line by line one after the other, when you call the function jq () it still had not been declared ..

'sorry for my english'

发布评论

评论列表(0)

  1. 暂无评论