问答详情
源自:8-1 概念与继承

为什么我按老师的代码写,却没有输出相应的值,name,age都是undefined呢?求帮助

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>OOP</title>

</head>

<body>

<script type="text/javascript">

function Person(name,age){

this.name = name;

this.age = age;

}

Person.prototype.hi = function(){

document.write('Hi,my name is '+this.name+",I'm "+age+' years old now'+"<br>");

};

Person.prototype.LEGS_NUM = 2;

Person.prototype.ARMS_NUM = 2;

Person.prototype.walk = function(){

document.write(this.name+" is walking..."+"<br>");

};

function Student(name, age, className){

Person.call(this.name,age);

this.className = className;

}

Student.prototype = Object.create(Person.prototype);

Student.prototype.constructor = Student;

Student.prototype.hi = function(){

document.write('Hi,my name is '+this.name+",I'm "+age+' years old now, and from '+this.className+"<br>");

};

Student.prototype.learn = function(subject){

document.write(this.name+' is learning '+subject+' at '+this.className+"<br>");

};

var jack = new Student('Jack',10,'Class 1,Grade 3'+"<br>");

jack.hi();

document.write(jack.LEGS_NUM+"<br>");

jack.walk();

jack.learn('Chinese');

</script>

</body>

</html>


提问者:小不点fly 2016-03-30 10:50

个回答

  • qq_覗水翛_0
    2016-03-30 18:42:40
    已采纳

    你有两个地方错了,一个地方少了一个逗号,一个地方少了一个this,看图,改过来过后可以正常显示。http://img.mukewang.com/56fbad920001ab2710400204.jpg