| | |
My two constructors are not working any help please
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hello, my task was to make two constructors for the code that I also posted and they are not working. Can somebody help me figure out why they are not working. I posted them right below.
here are my constructors for course and Faculty
course.js:
The HTML drivers to run the code:
here are my constructors for course and Faculty
course.js:
javascript Syntax (Toggle Plain Text)
function Course (name, times, instructorName, instructorDept ) { this.name = name ||"unknown course name"; this.times = times||"unknown time"; this.instructor = new Faculty(instructorName, instructorDept); this.getname = function(); this.setname = function( name ); this.gettimes = function(); this.settimes = function( dept ); this.getinstructor() = function(); this.setinstructor(Faculty); this.name.toString = function(); } faculty.js: function Faculty(name, dept) { this.name = name || "unknown"; this.dept = dept || "unknown"; this.getname = function(); this.setname = function( name ); this.getdata = function(); this.setdata = function( dept ); this.name.toString = function(); this.dept.toString = function(); } Here is the test file testprob1.js: var fac1 = new Faculty( "Dr. Smith", "Math" ); document.writeln( "New faculty: " + fac1.getname() + ", " + fac1.getdept() + "<br>" ); document.writeln( "Again: " + fac1 + "<br><br>" ); var fac2 = new Faculty(); document.writeln( "New faculty: " + fac2.getname() + ", " + fac2.getdept() + "<br>" ); fac2.setname( "Dr. Jones" ); fac2.setdept( "CS" ); document.writeln( "Updated: " + fac2.getname() + " " + fac2.getdept() + "<br><br>" ); var crs1 = new Course( "COMP322", "MWF 1:00-1:50", "Dr. Green", "CS" ); document.writeln( "New course: " + crs1.getname() + ", " + crs1.gettimes() + ", " + crs1.getinstructor() + "<br>" ); document.writeln( "Again: " + crs1 + "<br><br>" ); var crs2 = new Course(); document.writeln( "New course: " + crs2.getname() + ", " + crs2.gettimes() + ", " + crs2.getinstructor() + "<br><br>" ); crs2.setname( "COMP590" ); crs2.settimes( "TR 1:00-2:15" ); crs2.setinstructor( fac2 ); document.writeln( "Updated: " + crs2.getname() + ", " + crs2.gettimes() + ", " + crs2.getinstructor() );
The HTML drivers to run the code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Assignment 3, Problem 3</title> <script type="text/javascript" src="faculty.js"> </script> <script type="text/javascript" src="course.js"> </script> <script type="text/javascript" src="testProb1.js"> </script> </head> </html>
Last edited by Tekmaven; Jun 7th, 2009 at 10:05 pm. Reason: Code Tags
I've shortened out your code and skipped some of the unecessary lines inside the two constructors.
You'll need to run this entire document, to see the actual effects of the two functions.
Here's the code:
Tested in OPERA8+
You'll need to run this entire document, to see the actual effects of the two functions.
Here's the code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html id="html40L" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>Live Help!</title> <style type="text/css"> <!-- p { border-bottom : 2px solid #eee; color : #800080; line-height : 1.6; padding-bottom : .500em; margin-left: .700em; letter-spacing : 3px; margin-top : 2em; white-space : nowrap; } span { color : #365d95; } --> </style> <script type="text/javascript"> <!-- var Faculty = function( name, dept ) { this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" ); this.dept = (( dept ) ? dept : "undefined " + ( typeof( this ) ) + ": dept" ); /* Commented Block Started this.getname = function( name ) { }; // Creating Empty function. And also you forgot to include brackets, so that's why it's messing the whole program. this.setname = function( name ) { }; this.getdata = function() { }; this.setdata = function( dept ) { }; this.name.toString = function() { }; this.dept.toString = function() { }; */// Commented Block Ended }; var Course = function( name, times, instructorName, instructorDept ) { var instructor; this.name = (( name ) ? name : "undefined " + ( typeof( this ) ) + ": name" ); this.times = (( times ) ? times : "undefined " + ( typeof( this ) ) + ": times" ); this.instructor = new Faculty( instructorName, instructorDept ); /* this.getname = function() { }; this.setname = function( name ) { }; this.gettimes = function() { }; this.settimes = function( dept ) { }; this.getinstructor() = function(); this.setinstructor(Faculty) { }; this.name.toString = function() { }; */ }; // --> </script> </head> <body> <div id="main"> <script type="text/javascript"> <!-- /* Contructor Faculty() Usage Example: */ var fac1 = new Faculty("Dr. Smith", "Math" ); document.writeln("\n<p>New Faculty: <span>" + (( fac1.name ) ? fac1.name : fac1["name"] ) + "</span><br>Department: <span>" + (( fac1.dept ) ? fac1.dept : fac1["dept"] ) + "</span><br>" ); document.writeln("Again: <span>" + fac1.name + "</span>, <span>" + fac1.dept + "</span></p>\n" ); var fac2 = new Faculty(); fac2["name"] = "Dr. Jones"; fac2["dept"] = "CS"; document.writeln("\n<p>New Faculty: <span>" + (( fac2.name ) ? fac2.name : fac2["name"] ) + "</span><br>Department: <span>" + (( fac2.dept ) ? fac2.dept : fac2["dept"] ) + "</span></p>\n" ); var crs1 = new Course("COMP332", "MWF 1: 00-1:50", "Dr. Green", "CS" ); document.writeln("<p>New course: <span>" + (( crs1.name ) ? crs1.name : crs1["name"] ) + "</span><br>Time: <span>" + (( crs1.times ) ? crs1.times : crs1["times"] ) + "</span><br>Instructor: <span>" ); document.writeln( crs1.instructor.name + "</span><br>" + "Department: <span>" + crs1.instructor.dept + "</span></p>" ); // --> </script> </div> </div> </body> </html>
Tested in OPERA8+
![]() |
Similar Threads
- type 'byte' has no constructors vb.net (ASP.NET)
- Flash Player stop working since Ad-aware (Web Browsers)
- Internet Explorer not working. (Web Browsers)
- Flash Player stop working and won't reinstall (Windows NT / 2000 / XP)
- Taking address of constructors?? (C++)
- messanger not working. (Web Browsers)
- cd burner not working (Storage)
- Working on new design (DaniWeb Community Feedback)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: The use of isNAN
- Next Thread: Using AJAX results in PHP form
| Thread Tools | Search this Thread |
ajax ajaxexample ajaxjspservlets array browser bug captcha captchaformproblem cart checkbox child class close codes cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps marquee masterpage math matrixcaptcha media menu object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php position post programming progressbar prototype rated redirect runtime safari scale scriptlets scroll search security session shopping size software star stars synchronous toggle unicode variables web webservice wysiwyg \n





