...

View Full Version : Learning javascript, Help



TehMadHatter
01-17-2012, 04:31 PM
<HTML>

<HEAD>

<SCRIPT TYPE="text/javascript">

//Define Functions
function student(name,age,mother) {
this.name = name;
this.age = age;
this.mother = mother;
this.displayProfile = displayProfile;
}

function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
}

function displayProfile() {
document.writeln("Name: " + this.name + ");
document.writeln("Age: " + this.age + ");
document.writeln("Mother's name: " + this.mother + ");
document.writeln("Math Grade: " + this.grade.math + ");
document.writeln("English Grade: " + this.grade.english + ");
document.writeln("Science Grade: " + this.grade.science + ");
}
</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
student1 = new student("John",18,"Diane");
johngrade = new grade(100,80,90);
student1.displayProfile();
}
</SCRIPT>
</BODY>
</HTML>

Alright, I am learning javascript and in the middle of experimenting with functions, I don't see why this does not work?

DaveyErwin
01-17-2012, 04:56 PM
<HTML>

<HEAD>

<SCRIPT TYPE="text/javascript">

//Define Functions
function student(name,age,mother) {
this.name = name;
this.age = age;
this.mother = mother;
this.displayStudent = function(){displayStudent.call(this)};
}

function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
this.displayGrade = function(){displayGrade.call(this)};
}

function displayStudent() {
document.writeln("Name: " + this.name );
document.writeln("Age: " + this.age );
document.writeln("Mother's name: " + this.mother );
}
function displayGrade() {alert('d')
document.writeln("Math Grade: " + this.math );
document.writeln("English Grade: " + this.english );
document.writeln("Science Grade: " + this.science );
}

</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
student1 = new student("John",18,"Diane");
johngrade = new grade(100,80,90);
student1.displayStudent();
johngrade.displayGrade();
}
</SCRIPT>
</BODY>
</HTML>

TehMadHatter
01-17-2012, 05:21 PM
For the challenge I must only use one function to output it all, not one for grade and one for info. It must be all in 1 function. (Doing this as an assignment)

Also, I note that you added
this.displayGrade = function(){displayGrade.call(this)};

Isn't the function() { } not needed? only the displayGrade.call(this) ??

And what does the displayGrade.call(this) exactly do? My instructor has not taught us .call yet so I think there must be a way to do it without it?

DaveyErwin
01-17-2012, 07:27 PM
[QUOTE=TehMadHatter;1181951]For the challenge I must only use one function to output it all, not one for grade and one for info. It must be all in 1 function. QUOTE]

Well sorry, I guess this won't help.


//Define Functions
function student(name,age,mother) {
this.name = name;
this.age = age;
this.mother = mother;
this.display = function(){display.call(this)};
}

function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
this.display = function(){display.call(this)};
}

function display() {
for(var a in this)
if(typeof this[a] != "function")
document.write(a+":"+this[a]+"<br>");
}

</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
student1 = new student("John",18,"Diane");
johngrade = new grade(100,80,90);
student1.display();
johngrade.display();
}

maybe like this ...



//<HTML>

<HEAD>

<SCRIPT TYPE="text/javascript">

//Define Functions
function student(name,age,mother) {
this.name = name;
this.age = age;
this.mother = mother;
this.display = function(){display(this)};
}

function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
this.display = function(){display(this)};
}

function display(that) {
for(var a in that)
if(typeof that[a] != "function")
document.write(a+":"+that[a]+"<br>");
}

</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
student1 = new student("John",18,"Diane");
johngrade = new grade(100,80,90);
student1.display();
johngrade.display();
}
</SCRIPT>
</BODY>
</HTML>

xelawho
01-17-2012, 10:30 PM
like so?



<HTML>

<HEAD>

<SCRIPT TYPE="text/javascript">

//Define Functions
function student(name,age,mother) {
this.name = name;
this.age = age;
this.mother = mother;
this.displayProfile = displayProfile;
this.grade = new grade(100,80,90);
}

function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
}

function displayProfile() {
document.writeln("Name: " + this.name);
document.writeln("Age: " + this.age);
document.writeln("Mother's name: " + this.mother);
document.writeln("Math Grade: " + this.grade.math);
document.writeln("English Grade: " + this.grade.english);
document.writeln("Science Grade: " + this.grade.science);
}
</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
student1 = new student("John",18,"Diane");
student1.displayProfile();
}
</SCRIPT>
</BODY>
</HTML>

TehMadHatter
01-17-2012, 11:01 PM
I was able to complete it myself, this is what I was looking for:


<HTML>

<HEAD>

<SCRIPT TYPE="text/javascript">

//Define Functions
function student(name,age,grade) {
this.name = name;
this.age = age;
this.grade = grade;
this.display = display;
}
function grade(math,english,science) {
this.math = math;
this.english = english;
this.science = science;
}

function display() {
document.writeln("Name: " + this.name + "<BR /> Age: " + this.age + "<BR />");
document.writeln("Math grade: " + this.grade.math);
document.writeln("<BR />English grade: " + this.grade.english);
document.writeln("<BR />Science grade: " + this.grade.science);
}

</SCRIPT>

</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript">
{
johngrade = new grade(95,80,100);
student1 = new student("John",18, johngrade);
student1.display();
}
</SCRIPT>
</BODY>
</HTML>


Thanks though.

Dormilich
01-18-2012, 08:54 AM
And what does the displayGrade.call(this) exactly do?
call() is a method of the Function object (i.e. every function as instance of Function has this method), that allows to change the scope of the function (what you access by the keyword this).

ex. walk through a list of elements jQuery-like

var a = document.getElementsByTagName("a");
[].forEach.call(a, function(item, index, obj) {
item.onclick = doSomething;
});
ex. remove the first 2 parameters from a function call

function test()
{
// arguments is an object holding the call parameters
var args = [].splice.call(arguments, 0, 2);
alert(args.join(", "));
}
test(1, 2, 3, 4, 5);

Barber80
01-18-2012, 11:54 AM
!! thanks you guys...from the 4th time at last I could make it correctly...now it works!



_____________________
free photo editor (http://easyphotoeditor.org/)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum