Functions and Parameters

M

Martin Riat

Hello,

I have some trouble translating a little C++ program I made some years
ago into Javascript. I have defined a class myclass (more complicated
than the example here) and some functions in a similar manner to the
definition of the write function from my example.

Now I need a function that returns a Boolean value and takes 1 or 2
elements from the type myclass as parameters. Somebody can tell me how
I have to write that?

And the I need some functions which takes 1 or more entry parameters
of the type myclass and changes another element from the type myclass.
Something like a = my_function (a, b); How can I do that?

Many thanks for your suggestions.

Kind regards
Martin Riat

-----------------------

var COLUMNS=7;


function myclass () // class definition
{
this.element=Array(COLUMNS+1);
this.maxim;
this.saved=Boolean(true);

this.write=write_my;
// this.compare()=compare_my(); // ????
};


function compare_my(a,b) // does not work ???
// ----------------------------------
// compare 2 elements of myclass
// ----------------------------------
{
return (true);
}


function write_my()
// ----------------------------------
// writes the my_class to screen
// ----------------------------------
{
var counter=COLUMNS;
if (this.saved==true) {document.write("OK-")}
else {document.write("N--")};
for(counter=COLUMNS;counter>0;counter--)
{ document.write(this.element[counter], "<br>"); };
document.write(this.element[0]); // The last one
}


function main ()
{
var test=new myclass();
test.write();

// if (test.equal(test, test)==true) document.write ("Yes")
// else document.write ("No");

return(0);
}

dummy=main();
 
J

Julian Turner

function myclass () // class definition
{
this.element=Array(COLUMNS+1);

Should be: this.element=new Array();
You do not need to define any dimensions.
this.maxim;
this.saved=Boolean(true);

Should be: this.saved=new Boolean(true);
this.write=write_my;
// this.compare()=compare_my(); // ????

Should be: this.compare=compare_my;
The () effect a call on the method. Without the () you are handling a
reference to the method.
You got it right with the this.write method.
};


function compare_my(a,b) // does not work ???
// ----------------------------------
// compare 2 elements of myclass
// ----------------------------------

As this is a method, you use the "this" keyword to refer to the
instance of the object.

Thus: return this.a>this.b;
{
return (true);
}


function write_my()
// ----------------------------------
// writes the my_class to screen
// ----------------------------------
{
var counter=COLUMNS;
if (this.saved==true) {document.write("OK-")}
else {document.write("N--")};
for(counter=COLUMNS;counter>0;counter--)
{ document.write(this.element[counter], "<br>"); };
document.write(this.element[0]); // The last one
}


function main ()
{
var test=new myclass();

Note you have not inserted anything into the Array this.element in
this instance of your object, so there is nothing to write.

E.g

test.element[0]="First";
test.element[1]="Second";

Or shorthand

test.element=["First,"Second"];
test.write();

// if (test.equal(test, test)==true) document.write ("Yes")

Didn't you define the method as "compare" above, not "equal".
Why do you want to compare test with itself - i.e. "test,test"?
// else document.write ("No");

return(0);
}

dummy=main();

This last command will fire the main method as soon as the page is
loaded.

If you want to compare one instance with another you would do
something like:-

myclass.prototype.equal=function(oOther)
{
if (this.element.length!=oOther.element.length){return false;}

for (var i=0; i<this.element.length; i++)
{
if (this.element!=oOther.element){return false;}
}
return true;
}

var oTest1=new myclass();
oTest1.element=["First,"Second"];

var oTest2=new myclass();
oTest2.element=["First,"Second"];

window.alert(oTest1.equal(oTest2));
 
S

Steve van Dongen

Should be: this.element=new Array();
You do not need to define any dimensions.


Should be: this.saved=new Boolean(true);

That makes a Boolean object and usually that's not particularly
useful. If you have a and b
a = new Boolean(true);
b = new Boolean(true);
the expression ( a == b ) evaluates to false because they are
different objects. It is ( a.valueOf() == b.valueOf ) that evaluates
to true.

this.saved = Boolean(true) is redundant because it's converting a
boolean to a boolean. this.saved = true is good enough.

Regards,
Steve
 
J

Julian Turner

That makes a Boolean object and usually that's not particularly
useful. If you have a and b
a = new Boolean(true);
b = new Boolean(true);
the expression ( a == b ) evaluates to false because they are
different objects. It is ( a.valueOf() == b.valueOf ) that evaluates
to true.

Ahh, yes. Of course, I am creating an object rather than a literal.
Thank you for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top