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

I have a code below and when i try to run get the error when i call the function . I wonder hey this hi

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'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744254903a4565348.html

相关推荐

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

    I have a code below and when i try to run get the error when i call the function . I wonder hey this hi

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信