Functions and Parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Riat

    #1

    Functions and Parameters

    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=Ar ray(COLUMNS+1);
    this.maxim;
    this.saved=Bool ean(true);

    this.write=writ e_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==tr ue) {document.write ("OK-")}
    else {document.write ("N--")};
    for(counter=COL UMNS;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(tes t, test)==true) document.write ("Yes")
    // else document.write ("No");

    return(0);
    }

    dummy=main();



  • Robert

    #2
    Re: Functions and Parameters

    In article <h49np0d7jspk7a tvag34neledg5pc iouge@4ax.com>,
    Martin Riat <rrss1953@hotpo p.com> wrote:
    [color=blue]
    > 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.[/color]

    First off, I'll mention how to see your generated HTML. Second, I'll
    copy the changed HTML file.

    There are a number of one line Javascript programs you can use to
    display your generated Javascript. They have appeared in this forum
    that will display the generated html. Use the search term one liner
    against this forum.

    Or see:

    g.google.com

    Here is what I was able to do:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Workin g with Classes</title>

    <script type="text/javascript">

    // By convention, folks upper case the first letter the
    // constructor function.

    function Myclass ()
    {
    this.element = [ ]; // Declare an array

    // Copy the function arguements to the element array
    for (var i = 0; i<arguments.len gth; i++)
    { this.element[i] = arguments[i];}

    this.maxim = ""; // need some value here.
    this.saved = true;

    // this.write=writ e_my;
    // The above will work, but you may also do assignment
    // with a prototype statement

    this.compare = compare_my;


    }

    // ----------------------------------
    // compare 2 elements of myclass
    // ----------------------------------
    function compare_my(a)
    {

    if (this.element.l ength == a.element.lengt h &&
    this.maxium == a.maxium &&
    this.saved == a.saved &&
    same(this.eleme nt, a.element) )
    { return true; }
    else
    { return false; }

    function same(a, b)
    {

    for (var i = 0; i < a.length; i++)
    {
    if ( a[i] != b[i] )
    return false;
    }

    return true;
    }

    } // of compare


    // ----------------------------------
    // writes the my_class to screen
    //
    // This function may only be used when
    // the file is loading.
    //
    // The function innerHTML may be used to
    // change the display after the document is
    // loaded.
    //
    // ----------------------------------
    Myclass.prototy pe.write = function()
    {
    if (this.saved == true)
    {document.write ("OK-");}
    else
    {document.write ("N--");}
    document.write( "<br>Displa y the array:<br>");

    for(var counter=this.el ement.length - 1; counter>0; counter--)
    { document.write( this.element[counter] + "<br>"); }

    document.write( this.element[0]); // The last one
    }

    function main ()
    {
    var test = new Myclass("one"," two","three");
    // Folks like to upper case
    // letter of a class variable.

    test.write();

    var second = new Myclass("one"," two","diff");
    document.write( "<br>Variab les test and secord are ")
    if (test.compare(s econd) )
    { document.write ("equal."); }
    else
    { document.write ("different. "); }

    var third = new Myclass("one"," two","three");
    document.write( "<br>Variab les test and third are ")
    if (test.compare(t hird) )
    { document.write ("equal."); }
    else
    { document.write ("different. "); }

    var last = new Myclass("one"," two");
    document.write( "<br>Variab les test and last are ")
    if (test.compare(l ast) )
    { document.write ("equal."); }
    else
    { document.write ("different. "); }


    return; // no need to return something
    }
    </script>

    </head>

    <body>
    <p>Work with classes.</p>
    <p>Here is some generated data...<br>
    <script type="text/javascript">
    main();
    </script>
    </p>
    </body
    </html>

    Comment

    • Julian Turner

      #3
      Re: Functions and Parameters

      > function myclass () // class definition[color=blue]
      > {
      > this.element=Ar ray(COLUMNS+1);[/color]

      Should be: this.element=ne w Array();
      You do not need to define any dimensions.
      [color=blue]
      > this.maxim;
      > this.saved=Bool ean(true);[/color]

      Should be: this.saved=new Boolean(true);
      [color=blue]
      >
      > this.write=writ e_my;
      > // this.compare()= compare_my(); // ????[/color]

      Should be: this.compare=co mpare_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.
      [color=blue]
      > };
      >
      >
      > function compare_my(a,b) // does not work ???
      > // ----------------------------------
      > // compare 2 elements of myclass
      > // ----------------------------------[/color]

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

      Thus: return this.a>this.b;
      [color=blue]
      > {
      > return (true);
      > }
      >
      >
      > function write_my()
      > // ----------------------------------
      > // writes the my_class to screen
      > // ----------------------------------
      > {
      > var counter=COLUMNS ;
      > if (this.saved==tr ue) {document.write ("OK-")}
      > else {document.write ("N--")};
      > for(counter=COL UMNS;counter>0; counter--)
      > { document.write( this.element[counter], "<br>"); };
      > document.write( this.element[0]); // The last one
      > }
      >
      >
      > function main ()
      > {
      > var test=new myclass();[/color]

      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,"Sec ond"];
      [color=blue]
      > test.write();
      >
      > // if (test.equal(tes t, test)==true) document.write ("Yes")[/color]

      Didn't you define the method as "compare" above, not "equal".
      Why do you want to compare test with itself - i.e. "test,test" ?
      [color=blue]
      > // else document.write ("No");
      >
      > return(0);
      > }
      >
      > dummy=main();[/color]

      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.prototy pe.equal=functi on(oOther)
      {
      if (this.element.l ength!=oOther.e lement.length){ return false;}

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

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

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

      window.alert(oT est1.equal(oTes t2));

      Comment

      • Steve van Dongen

        #4
        Re: Functions and Parameters

        julian@baconbut ty.com (Julian Turner) wrote:
        [color=blue][color=green]
        >> function myclass () // class definition
        >> {
        >> this.element=Ar ray(COLUMNS+1);[/color]
        >
        >Should be: this.element=ne w Array();
        >You do not need to define any dimensions.
        >[color=green]
        >> this.maxim;
        >> this.saved=Bool ean(true);[/color]
        >
        >Should be: this.saved=new Boolean(true);[/color]

        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

        Comment

        • Julian Turner

          #5
          Re: Functions and Parameters

          > That makes a Boolean object and usually that's not particularly[color=blue]
          > 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.[/color]

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

          Comment

          Working...