我是 Javascript 类的新手,但我尝试使用 Raphael 创建一个简单的绘图应用程序。我目前遇到的问题是一些代码在同一个文件中时有效,但是当我尝试将其取出并包含它时,它给了我:
Uncaught TypeError: undefined is not a function.
我的第一个想法是文件没有被正确包含,所以我检查了,chrome 的开发帮助工具显示资源很好。
代码位于名为 line.js 的文件中:
function line(paper) {
this.x1 = null;
this.x2 = null;
this.y1 = null;
this.y2 = null;
this.paper = paper;
};
line.prototype.draw = function(){
this.paper.path(
"M " + this.x1 + " " + this.y1 +
" l " + (this.x2 - this.x1) + " " + (this.y2 - this.y1) + " z"
);
};
并且正在使用它的代码是:
<script type="text/javascript" src="../js/raphael-min2.1.js"></script>
<script type="text/javascript" src="../js/line.js"></script>
<script>
window.onload = function() {
var paper = new Raphael($('#canvas')[0], 500, 500);
var canvas =$('#canvas');
var line = new line(paper);
canvas.mousedown(function(e){
line.x1 = e.offsetX;
line.y1 = e.offsetY;
});
canvas.mouseup(function(e){
line.x2 = e.offsetX;
line.y2 = e.offsetY;
line.draw();
});
};
</script>
我以前从未在 javascript 中使用过类,也没有包含我自己制作的 js 文件,所以对此事的任何了解,不仅仅是修复会很好。此外,如果您认为有更好的方法来处理我通常如何做到这一点,请告诉我!非常感谢!