function inside function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    function inside function

    how do i add a function i want to use inside another js function belonging to js class ?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Can you explain with an example?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      i have a class eg myClass
      then i have a function that belongs to that class myClass.prototy pe.display()=fu nction{ here code to display and here i want to use two other functions}
      that also belong to the class myClass

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        [CODE=javascript] <script language="javas cript" src="source.js" >[/CODE] ?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by oll3i
          i have a class eg myClass
          then i have a function that belongs to that class myClass.prototy pe.display()=fu nction{ here code to display and here i want to use two other functions}
          that also belong to the class myClass
          Sorry I posted that before the explanation. If the functions are in the same class then you should be able to access them by just this.func()

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by r035198x
            [CODE=javascript] <script language="javas cript" src="source.js" >[/CODE] ?
            Tut, tut! The language attribute is deprecated - should be:
            Code:
            <script type="text/javascript" ...></script>

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              but when i define
              Code:
              Menu.prototype.changeLinkStyleOn = function(id){
              var divId= document.getElementById(id);
              with (divId.style){
              width:7.5em;
              float:left;
              display:block;
              text-align:center;
              margin:0 1px 0 0;
              font-size:100%;
              text-decoration:none;
              color:#FFF;
              font-weight:bold;
              background: url(../images/top_menu_hover.gif) left top no-repeat;  
              }
              };
              i get an error that the Menu is not defined

              and Menu is defined
              as

              Code:
              function Menu(modul,var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12,var13,var14,var15)
              {
              this.modul=modul;
              this.var1=var1;
              this.var2=var2;
              this.var3=var3;
              this.var4=var4;
              this.var5=var5;
              this.var6=var6;
              this.var7=var7;
              this.var8=var8;
              this.var9=var9;
              this.var10=var10;
              this.var11=var11;
              this.var12=var12;
              this.var13=var13;
              this.var14=var14;
              this.var15=var15;
              };
              do i add semicolon at the end of the class definition

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                my js file is included
                Code:
                <script type="text/javascript" src="../js/menu.js"></script>

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  but if i cut out these two classes (Menu.prototype .changeLinkStyl eOn = function(id) and Menu.prototype. changeLinkStyle Out= function(id) )and leave only Menu class definition and Menu.prototype. display= function() definition the menu displays?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    The problem is with your syntax. Forget the with shortcut. Also don't forget that JavaScript style names are camel-case, e.g. text-align becomes style.textAlign .

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #11
                      thank You i will try it out when i 'm back

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by acoder
                        Tut, tut! The language attribute is deprecated - should be:
                        Code:
                        <script type="text/javascript" ...></script>
                        Yuck, thanks for that.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by r035198x
                          Yuck, thanks for that.
                          No problem :)

                          Comment

                          • oll3i
                            Contributor
                            • Mar 2007
                            • 679

                            #14
                            still the same doesnt see Menu function ? but again when i remove functions

                            Menu.prototype. changeLinkStyle On = function(id) and Menu.prototype. changeLinkStyle Out= function(id) menu displays


                            Menu.prototype. changeLinkStyle On = function(id) is defined as follows
                            Code:
                            Menu.prototype.changeLinkStyleOn = function(id){
                            var div= document.getElementById(id);
                            div.style.width:8em;
                            div.style.float:'left';
                            div.style.display:'block';
                            div.style.textAlign:'center';
                            div.style.marginTop:0;
                            div.style.marginRight:0;
                            div.style.marginBottom:0;
                            div.style.marginLeft:0;
                            div.style.fontSize:100%;
                            div.style.textDecoration:'none';
                            div.style.color:'#FFF';
                            div.style.fontWeight:'bold';
                            div.style.background: url(../images/top_menu_hover.gif) left top no-repeat;  
                            };
                            i dont know how to camel the "background : url" and if i should?

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Use = instead of :

                              Don't forget to put the values in quotes too.

                              Comment

                              Working...