下面的输出次序为156,18,undifined,我想知道为什么最后有个undifined呢??
<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
    <title>Test</title>
<script language="JavaScript">
 function person(){
this.name="张三";
}
function student(){
var age=18;
student.prototype.getage=function(){
alert(age);
}
}
function test(){ student.prototype = new person();
student.prototype.age=156;
var s=new student();alert(s.age);
alert(s.getage());}
</script> </head>
<body>
<a href="#" onclick="test()"> 链接一</a> </body>
</html>

解决方案 »

  1.   

    举个例子
    function Person(){ } 
      Person.prototype.hello = "hello"; 
      Person.prototype.sayHello = function(){  alert(this.hello); } 
      
      function Student(){ } 
      Student.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Student,从而实现了继承 
      Student.prototype.world = "world"; 
      Student.prototype.sayWorld = function(){  alert(this.world); } 
      
      var c = new Student(); 
      c.sayHello(); 
      c.sayWorld(); 
      

  2.   

    alert(s.getage());s.getage()  // 弹出18 返回voidalert(s.getage());等价于 alert(alert(18))  alert(18)无返回值 当然undefined了