Javascript错误:对象未定义

nan*_*oft 0 javascript

我用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>
Run Code Online (Sandbox Code Playgroud)

提前致谢....

Imp*_*Imp 5

var testCircle = mycircle(3, 4, 5);
Run Code Online (Sandbox Code Playgroud)

应该

var testCircle = new mycircle(3, 4, 5);
Run Code Online (Sandbox Code Playgroud)

使用new关键字调用构造函数.如果未使用关键字,则指定mycircle函数的返回值.并且由于不mycircle包含任何return语句,返回值是undefined- 这是您testCircle在代码中分配的内容.