Isaac
asked on
SCRIPT5007: Unable to get property '0' of undefined or null reference
Why do I get an error on this line? "SCRIPT5007: Unable to get property '0' of undefined or null reference "
console.log("Class 1: "+newStudent.Classes[0]);
console.log("Class 1: "+newStudent.Classes[0]);
function student(firstName,lastName,ssn,dormName)
{
this.fname = firstName;
this.lname = lastName;
this._ssn = ssn || 0;
this.classes = [];
this.dName = dormName || "A Hall";
};
// Individually add methods to the prototype
student.prototype.addClasses = function(className, roomNumber) {
this.classes.push({className: className, roomNumber: roomNumber});
};
Object.defineProperty(student.prototype, "fullname", {
get: function() {
return this.fname + " " + this.lname;
}
});
var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
console.log("HELLO WORLD");
console.log(newStudent.fullname);
newStudent.addClasses("Algebra", "33B");
newStudent.addClasses("Geometry", "44B");
newStudent.addClasses("History", "12B");
newStudent.addClasses("Sociology", "144C");
newStudent.addClasses("Biology", "312D");
newStudent.addClasses("Calculus", "98S");
newStudent.addClasses("Statistics", "55B");
console.log("Number of classes: " + newStudent.classes.length);
console.log("Class 1: "+newStudent.Classes[0]);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's another question :)
I guess it's because it does not recognize the object you've inserted.
try printing newStudent.classes[0].clas sName - to get the class name and roomNumber to get the room number
I guess it's because it does not recognize the object you've inserted.
try printing newStudent.classes[0].clas
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And to list all of the classes...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>JS Classes</title>
</head>
<body>
<h1>JS Classes</h1>
<script type="text/javascript">
<!--
function student(firstName,lastName,ssn,dormName)
{
this.fname = firstName;
this.lname = lastName;
this._ssn = ssn || 0;
this.classes = [];
this.dName = dormName || "A Hall";
};
// Individually add methods to the prototype
student.prototype.addClasses = function(className, roomNumber) {
this.classes.push({className: className, roomNumber: roomNumber});
};
Object.defineProperty(student.prototype, "fullname", {
get: function() {
return this.fname + " " + this.lname;
}
});
var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
console.log("HELLO WORLD");
console.log(newStudent.fullname);
newStudent.addClasses("Algebra", "33B");
newStudent.addClasses("Geometry", "44B");
newStudent.addClasses("History", "12B");
newStudent.addClasses("Sociology", "144C");
newStudent.addClasses("Biology", "312D");
newStudent.addClasses("Calculus", "98S");
newStudent.addClasses("Statistics", "55B");
console.log("Number of classes: " + newStudent.classes.length);
document.write("Number of classes: " + newStudent.classes.length + "<br>");
for(var ii=0;ii<newStudent.classes.length;ii++) {
console.log("Class "+(ii+1)+": "+newStudent.classes[ii]);
document.write("Class "+(ii+1)+": "+newStudent.classes[ii].className+" "+newStudent.classes[ii].roomNumber + "<br>");
}
// -->
</script>
</body>
</html>
ASKER