我有以下带有 javascript 的 Html 文件。这给了我错误“testCircle 未定义”。 kinldy 帮我解决这个问题。
<html>
<body>
<h1> Testing Object-based Javascipt </h1>
<script type="text/javascript">
function mycircle(x,y,r)
{
this.xcoord=x;
this.ycoord=y;
this.radius=r;
this.area = getArea;
this.getCircumference = function () { return (2 * Math.PI * this.radius ) ; };
}
function getArea()
{
return (Math.PI * this.radius * this.radius);
}
var testCircle = mycircle(3,4,5);
window.alert('The radius of my circle is ' + testCircle.radius);
</script>
</body>
</html>
提前致谢......
最佳答案
var testCircle = mycircle(3, 4, 5);
应该是
var testCircle = new mycircle(3, 4, 5);
构造函数将使用new
关键字调用。如果未使用关键字,则您将分配 mycircle
函数的返回值。由于 mycircle
不包含 return
语句,因此返回值是 undefined
- 这就是您在中分配给 testCircle
的值代码。
关于javascript - JavaScript 错误 : Object not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675505/