Javascript bug

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
I'm really lost on this homework on Javascript and I call upon the mighty programming gurus of AT for help. The point of the function is to output "100 101 102." In Java or C++, the code would normally output "100 101 102," but in Javascript it outputs "100 101 101." I know how to fix the code to output properly, but I don't know why.
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Try passing in the object and the function separately to functionExecutor. Like this:
functionExecutor(obj, inc)

function functionExecutor(obj, fnc)
{
obj.fnc();
}
 

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
Originally posted by: joshsquall
Try passing in the object and the function separately to functionExecutor. Like this:
functionExecutor(obj, inc)

function functionExecutor(obj, fnc)
{
obj.fnc();
}

No, I know how to fix it. Although what you suggested doesn't work, you have to do fnc.call(obj); What I'm asking is why it prints "100 101 101" in Javascript.
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Originally posted by: neutralizer
Originally posted by: joshsquall
Try passing in the object and the function separately to functionExecutor. Like this:
functionExecutor(obj, inc)

function functionExecutor(obj, fnc)
{
obj.fnc();
}

No, I know how to fix it. Although what you suggested doesn't work, you have to do fnc.call(obj); What I'm asking is why it prints "100 101 101" in Javascript.

It's passing the object by value, not reference. It is incrementing the value inside of the functionExecutor, but that isn't stored in the same memory location as the original obj. Try printing the value inside functionExecutor after you increment it. Should say 102.
 

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
Thanks for the help, joshsquall. I have another homework problem in which there is an error in the code that can be fixed by editing a line. I know that in order to fix it, it should be Stack.prototype.top = blah, but I don't see the difference between methods defined in the function Stack and methods defined by adding a property top to Stack that points to a function.
 

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
Originally posted by: GundamSonicZeroX
You have a JavaScript class. I'm so jealous.

It's actually a class on compilers, but the languages we're working on is Javascript and AJAX.
 

jman19

Lifer
Nov 3, 2000
11,220
654
126
Originally posted by: neutralizer
Originally posted by: GundamSonicZeroX
You have a JavaScript class. I'm so jealous.

It's actually a class on compilers, but the languages we're working on is Javascript and AJAX.

A compilers class taught using Javascript? :confused:
 

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
Originally posted by: jman19
Originally posted by: neutralizer
Originally posted by: GundamSonicZeroX
You have a JavaScript class. I'm so jealous.

It's actually a class on compilers, but the languages we're working on is Javascript and AJAX.

A compilers class taught using Javascript? :confused:

Actually, it's a programming languages and compilers class. I do agree with your confusion on how a compilers class can be taught with Javascript since the language is interpreted rather than compiled.
 

jman19

Lifer
Nov 3, 2000
11,220
654
126
Originally posted by: neutralizer
Originally posted by: jman19
Originally posted by: neutralizer
Originally posted by: GundamSonicZeroX
You have a JavaScript class. I'm so jealous.

It's actually a class on compilers, but the languages we're working on is Javascript and AJAX.

A compilers class taught using Javascript? :confused:

Actually, it's a programming languages and compilers class. I do agree with your confusion on how a compilers class can be taught with Javascript since the language is interpreted rather than compiled.

Ah well I guess if you are doing a survey of sorts of programming languages then it makes a lot more sense that you are using Javascript.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
functionExecutor(obj.inc); // execute obj.inc

That literally passes the function, so functionExecutor sees "this.value++", so it increments the value property of whatever "this" happens to be. Note that in other languages, that may actually do functionExecutor(obj.inc()), running .inc() and passin the (bogus) result to functionExecutor.

You could do functionExecutor(function() { obj.inc(); }); if you wanted to... that would cause obj.inc() to be executed from within functionExecutor.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
Originally posted by: neutralizer
Originally posted by: GundamSonicZeroX
You have a JavaScript class. I'm so jealous.

It's actually a class on compilers, but the languages we're working on is Javascript and AJAX.

I hope...seriously, that your instructor didn't tell you that AJAX is a language.