admin管理员组文章数量:1388830
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
1 Answer
Reset to default 7Because 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.
本文标签: jqueryhow to fix undefined property in javascriptStack Overflow
版权声明:本文标题:jquery - how to fix undefined property in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744627050a2616321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论