Hi,
I’m learning how to write OO Javascript and cannot figure out what is going on with an error that I am getting.
I have a js file (objects.js) with a Person object and a student inheriting from it:
[CODE]
function Person(name, age){
var surname;
Person.prototype.getName = function(){ return name; }
Person.prototype.getAge = function(){ return age; }
Person.prototype.setName = function(newName){ name = newName; }
Person.prototype.setAge = function(newAge){ age = newAge; }
Person.prototype.getSurname = function() { return surname; }
Person.prototype.setSurname = function(newSurname) { surname = newSurname; }
Person.prototype.sayHi = function(){
alert(‘hi ‘ + this.getName());
}
Person.prototype.saySurname = function(){
alert(this.getSurname());
}
}
function Student(name, age, studentNumber){
Person.call(this, name, age);
Student.prototype = new Person();
Student.prototype.constructor = Student;
Student.prototype.getStudentNumber = function(){ return studentNumber; }
Student.prototype.setStudentNumber = function(newStudentNr){ studentNumber = newStudentNr; }
Student.prototype.sayHowdy = function(){
alert(‘Details: ‘ + this.getName() + ‘ ‘ + this.getAge() + ‘ ‘ + this.getSurname() + ‘ ‘ + this.getStudentNumber());
}
}
To exercise that code, I have knocked up the following HTML:
[CODE]
<html>
<head>
<script type=”text/javascript” src=’./objects.js’></script>
<script>
function CreatePerson(name, age){
var Bob = new Person(name, age);
Bob.sayHi();
}
function CreateStudent(name, age, studentNumber){
var Frank = new Student(name, age, studentNumber);
Frank.setSurname(‘Dobbs’);
Frank.sayHowdy();
}
</script>
</head>
<body>
<input id=”Button1″ type=”button” value=”CreatePerson” onclick=”CreatePerson(‘Bob’, 44); ” /><br />
<input id=”Button1″ type=”button” value=”CreateStudent” onclick=”CreateStudent(‘Frank’, 55, 1167596); ” /><br />
</body>
</html>
When I click on the [B]CreateStudent
[QUOTE]
Frank.setSurname is not a function
Can someone please help diagnose my error. I’d also like to know whether I could have a [I]sayHi
Cheers