Functions and Parameters

Discussion in 'Javascript' started by Martin Riat, Nov 17, 2004.

  1. Martin Riat

    Martin Riat Guest

    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();
    Martin Riat, Nov 17, 2004
    #1
    1. Advertising

  2. > 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));
    Julian Turner, Nov 18, 2004
    #2
    1. Advertising

  3. (Julian Turner) wrote:

    >> 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);


    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
    Steve van Dongen, Nov 19, 2004
    #3
  4. > 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.
    Julian Turner, Nov 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    967
    Jonck van der Kogel
    May 27, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,577
    Victor Bazarov
    Jun 21, 2004
  3. Jason
    Replies:
    2
    Views:
    493
    Jonathan Mcdougall
    May 13, 2006
  4. kito
    Replies:
    2
    Views:
    406
  5. Florian Loitsch
    Replies:
    11
    Views:
    242
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page