1

I've been trying to figure out this error in JS and cant understand why its happening. The program is supposed to inherit properties from the person object and add two additional properties, sID and courses. The for loop is then supposed to push values that the user inputs(a minimum of 5) into the properties. The problem I have though is that once it reaches the courses property, which is an array, I keep getting an error saying that courses is undefined. Any help is greatly appreciated.

    // person object
var person = { firstname: "", lastname: "", email: "" }; 
var student = {};
student.prototype = person;
student.prototype.sid = "";
student.prototype.courses = {};



/******************************************************************************
 * your code starts here
 *****************************************************************************/

var answer = null;
function getanswer(string) {
    answer = string;
    return answer;
}
do {
    getanswer(prompt("Please enter your first name, last name, student ID, email and courses (seperated by ',')"));
    var array = answer.split(',');
    answer = array;
    for(var i = 0; i <= answer.length; i++) {
        if (i < 4) {
            student[i] = answer[i];
            alert(student[i]);
        }
        if(i > 3) {
            student.courses[i] = answer[i];
            alert(student.courses[i]);
        }        
    }

} while(answer !== null || answer !== "" );
onemic
  • 59
  • 1
  • 3
  • 10
  • 3
    You can't modify the prototype of an instance at runtime, as you're trying to do. You need to create a new constructor function (`function Student(){}`), set the prototype on that (`Student.prototype = person`), and the instantiate it (`var student = new Student`). – Jeremy Oct 02 '14 at 22:02
  • @JeremyBanks you should add that as an answer. :) –  Oct 02 '14 at 22:03
  • @Phaeze Sure. Doing so now. – Jeremy Oct 02 '14 at 22:05
  • Looks like person has instance specific members that you are putting on Student.prototype. since they are immutable it will work but doesn't show right intention. More info about this can be found here: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Oct 03 '14 at 00:49

1 Answers1

2
var student = {};
student.prototype = person;

You seem to be trying modify the prototype of the student instance after it's been created. Generally, this isn't possible in JavaScript. (There may be some non-standard APIs that support this, but you shouldn't be using them.) Also, the prototype of an object is not directly exposed as a public .prototype property on the object. If you want to read the prototype of an instance, you actually need to look it up as through its constructor, as .constructor.prototype (but don't try to modify that!).

To use JavaScript's prototypal inheritance, you need to create a constructor function, and set the prototype on that. For example, in your case:

function Student() {
}
Student.prototype = person;

You can then use this constructor to create a Student instance which will inherit values from person:

var student = new Student;
Jeremy
  • 1
  • 85
  • 340
  • 366