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

jquery - how to fix undefined property in javascript? - Stack Overflow

programmeradmin3浏览0评论

i have this script:

var Tord = (function () {

    Tord.prototype.getHeader = function () {
        alert('test');
    };

    return Tord;
})();

$(document).ready(function() {

    var tod = new Tord();
    tod.getHeader();
});

i get an error Uncaught TypeError: Cannot read property 'prototype' of undefined line 3

i don't understand why?

here is how my js files are loaded:

    <link rel="stylesheet" href="public/jquery.mobile/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="public/style.css" />
    <script src="public/jquery-1.8.3.min.js"></script>
    <script src="public/jquery.mobile/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="public/cordova-2.2.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="public/script.js"></script>

script.js has the script from above.

any ideas? thanks.

i have this script:

var Tord = (function () {

    Tord.prototype.getHeader = function () {
        alert('test');
    };

    return Tord;
})();

$(document).ready(function() {

    var tod = new Tord();
    tod.getHeader();
});

i get an error Uncaught TypeError: Cannot read property 'prototype' of undefined line 3

i don't understand why?

here is how my js files are loaded:

    <link rel="stylesheet" href="public/jquery.mobile/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="public/style.css" />
    <script src="public/jquery-1.8.3.min.js"></script>
    <script src="public/jquery.mobile/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="public/cordova-2.2.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="public/script.js"></script>

script.js has the script from above.

any ideas? thanks.

Share Improve this question asked Dec 23, 2012 at 9:06 PatrioticcowPatrioticcow 27.1k76 gold badges221 silver badges340 bronze badges 1
  • Tord should not be self invoking. – Christoph Commented Dec 23, 2012 at 9:10
Add a ment  | 

1 Answer 1

Reset to default 7

Because of exactly the code you've shown. Look, when this line is invoked...

Tord.prototype.getHeader = function () {

... Tord is undefined. But Tord should be a function when you set this (as prototype should be a property of constructor function to be of any use) - and you don't make any efforts to make Tord a function at all.

I think what you want to do may be achieved with this:

var Tord = (function () {
    var Tord = function() {}; // to properly name the constructor
    Tord.prototype.getHeader = function () {
        alert('test');
    };
    return Tord;
})();

With this you can define different constructors based on some external logic (but still define them once). But in the current state of your code there's no need to make it that plex: this...

var Tord = function() {};
Tord.prototype.getHeader = function() { ... };

... should suffice, in my opinion.

发布评论

评论列表(0)

  1. 暂无评论