How to call javascript object method from within object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eyeinstyin
    New Member
    • Sep 2007
    • 20

    How to call javascript object method from within object

    [CODE=javascript]
    function Circle(radius)
    {
    this.radius=rad ius;
    this.area=0;
    }
    Circle.prototyp e.area=function ()
    {
    if(radius>=1)
    this.area=this. radius*(i want to call circumference with argument=false)/2
    else
    this.area=this. radius*(i want to call circumference with argument=true)/2
    }
    Circle.prototyp e.circumference =function(varia ble)
    {
    if(variable)
    return 0;
    else
    return 2*3.14*(this.ra dius)*(this.rad ius);
    }
    [/CODE]
    this is just example .i dont need alternate code; but;how do i call circumfernce method.thats all And sorry if i posted wrong way or wrong format
    Last edited by gits; Sep 13 '07, 01:39 PM. Reason: fix code tags
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by Eyeinstyin
    [CODE=javascript]
    function Circle(radius)
    {
    this.radius=rad ius;
    this.area=0;
    }
    Circle.prototyp e.area=function ()
    {
    if(radius>=1)
    this.area=this. radius*(i want to call circumference with argument=false)/2
    else
    this.area=this. radius*(i want to call circumference with argument=true)/2
    }
    Circle.prototyp e.circumference =function(varia ble)
    {
    if(variable)
    return 0;
    else
    return 2*3.14*(this.ra dius)*(this.rad ius);
    }
    [/CODE]
    this is just example .i dont need alternate code; but;how do i call circumfernce method.thats all And sorry if i posted wrong way or wrong format
    Welcome to TSDN!
    I think you so much advanced in JavaScript.
    This is OOP supported bu JavaScript.
    So you can call it as happens in OOP.
    Anyway...!
    Try this, it will work.

    [code=javascript]
    var c = new Circle(100);
    //now to call circumfernce method.
    c.circumfernce( );
    [/code]

    That's All.
    Good Luck.

    Kind regards,
    Dmjpro.

    Comment

    • Eyeinstyin
      New Member
      • Sep 2007
      • 20

      #3
      Originally posted by dmjpro
      Welcome to TSDN!
      I think you so much advanced in JavaScript.
      This is OOP supported bu JavaScript.
      So you can call it as happens in OOP.
      Anyway...!
      Try this, it will work.

      [code=javascript]
      var c = new Circle(100);
      //now to call circumfernce method.
      c.circumfernce( );
      [/code]

      That's All.
      Good Luck.

      Kind regards,
      Dmjpro.
      Please refer my code.I want to call the object function(circum ference) from within that object function(radius ).Please help me if u can...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        i give an example and you will see what to do:

        [CODE=javascript]
        function OBJ() {
        }

        OBJ.prototype.m ethod_a = function(params ) {
        }

        OBJ.prototype.m ethod_b = function() {
        // we want to call method a here:
        this.method_a(p arams);
        }
        [/CODE]
        kind regards

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          hi ...........Eyei nstyin!

          Did you mean that what Gits says.

          Kind regards,
          Dmjpro.

          Comment

          • Eyeinstyin
            New Member
            • Sep 2007
            • 20

            #6
            Originally posted by gits
            hi ...

            i give an example and you will see what to do:

            [CODE=javascript]
            function OBJ() {
            }

            OBJ.prototype.m ethod_a = function(params ) {
            }

            OBJ.prototype.m ethod_b = function() {
            // we want to call method a here:
            this.method_a(p arams);
            }
            [/CODE]
            kind regards
            Thnx That work.Thanx both of u.Reply was quick
            But what i want is "onmousecli ck" event the object method should be called
            I specify as follows this.onmousecli ck=function(){ "function name"}.
            But how do i put the function of object for eg
            this.onmousecli ck=this.circumf erence(variable );//directly call circumference instead of calling it on mouseclick

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              hi ...

              its like a static call ... use:

              Code:
              onclick="OBJ.prototype.method_b(your_param_value);"
              kind regards

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                ahhrg ... i think i misunderstood your question? ... please give an example and specify what you want to do exactly ... post sample code please.

                kind regards

                Comment

                • Eyeinstyin
                  New Member
                  • Sep 2007
                  • 20

                  #9
                  [CODE=html]
                  <html>
                  <head>
                  <title></title>
                  </head>
                  <body>
                  <script type="text/javascript">
                  function fdiv(argument)
                  {
                  this.element=do cument.createEl ement('div');
                  this.element.in nerHTML="100";
                  this.func=this. Function1;

                  //*************** *************** *************** *************** *****
                  // this.element.on click=this.Func tion(argument); //i want this i.e i want to call Function1 with argument when i click the div
                  //*************** *************** *************** *************** ******8
                  this.element.st yle.backgroundC olor="RED";
                  document.body.a ppendChild(this .element);
                  }
                  fdiv.prototype. Function1=funct ion(argument)
                  {
                  if(argument)
                  this.Function2( argument);
                  else
                  this.Function2( argument);
                  }
                  fdiv.prototype. Function2=funct ion(argument)
                  {
                  alert(parseInt( this.element.in nerHTML)+parseI nt(argument));
                  }
                  var temp=new fdiv(2);
                  </script>

                  </body>
                  </html>
                  [/CODE]
                  DIV is ready.When div is clicked Function1 is to be called
                  Last edited by gits; Sep 13 '07, 01:38 PM. Reason: fix code tags

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    hi ...

                    i see ... try this:

                    [HTML]<html>
                    <head>
                    <title></title>
                    </head>
                    <body>
                    <script type="text/javascript">
                    function fdiv(argument)
                    {
                    this.element=do cument.createEl ement('div');
                    this.element.in nerHTML="100";
                    this.func=this. Function1;

                    // we need this to fix the scope in the anonymous function
                    // that is used for onclick
                    var me = this;

                    // use an anonymous function here
                    this.element.on click = function() { me.Function1(ar gument); };

                    this.element.st yle.backgroundC olor="RED";

                    document.body.a ppendChild(this .element);
                    }

                    fdiv.prototype. Function1=funct ion(argument)
                    {
                    if(argument)
                    this.Function2( argument);
                    else
                    this.Function2( argument);
                    }

                    fdiv.prototype. Function2=funct ion(argument)
                    {
                    alert(parseInt( this.element.in nerHTML)+parseI nt(argument));
                    }

                    var temp=new fdiv(2);
                    </script>

                    </body>
                    </html>
                    [/HTML]
                    kind regards

                    Comment

                    • Eyeinstyin
                      New Member
                      • Sep 2007
                      • 20

                      #11
                      Originally posted by gits
                      hi ...

                      i see ... try this:

                      [HTML]<html>
                      <head>
                      <title></title>
                      </head>
                      <body>
                      <script type="text/javascript">
                      function fdiv(argument)
                      {
                      this.element=do cument.createEl ement('div');
                      this.element.in nerHTML="100";
                      this.func=this. Function1;

                      // we need this to fix the scope in the anonymous function
                      // that is used for onclick
                      var me = this;

                      // use an anonymous function here
                      this.element.on click = function() { me.Function1(ar gument); };

                      this.element.st yle.backgroundC olor="RED";

                      document.body.a ppendChild(this .element);
                      }

                      fdiv.prototype. Function1=funct ion(argument)
                      {
                      if(argument)
                      this.Function2( argument);
                      else
                      this.Function2( argument);
                      }

                      fdiv.prototype. Function2=funct ion(argument)
                      {
                      alert(parseInt( this.element.in nerHTML)+parseI nt(argument));
                      }

                      var temp=new fdiv(2);
                      </script>

                      </body>
                      </html>
                      [/HTML]
                      kind regards
                      no other way huh.creating dummy variable ....

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        what do you mean? ... the me-variable? it is a common way to fix the execution context for anonymous functions ...

                        kind regards

                        Comment

                        • Eyeinstyin
                          New Member
                          • Sep 2007
                          • 20

                          #13
                          Originally posted by gits
                          what do you mean? ... the me-variable? it is a common way to fix the execution context for anonymous functions ...

                          kind regards
                          ya 'me' variable .Thnx i got my program working

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5388

                            #14
                            hi ...

                            glad to hear you got it working ... post back to the forum anytime you have more questions :)

                            kind regards

                            Comment

                            Working...