SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Modify local variable?

    I have always been lead to believe that local variable can only be modified by the function in which they are called! And global variables exactly the opposite! But I have found a way in which it is possible! Im not saying its a bad thing I very happy to find this out in fact! But why is this possible doesnt it contradict the whole idea global and local variables!

    Code:
    <html>
    <head>
    <script>
    function bttnInfo(){
       var isClicked, id;
       this.isClicked=false;
       this.is=null;
    } 
    bttnInfo=new bttnInfo();
    function clicked(id){
       if(bttnInfo.isClicked){
     bttnInfo.isClicked=false;
     bttnInfo.id=null;
     alert(bttnInfo.isClicked);
     alert(bttnInfo.id);
    	}
    	else{
     bttnInfo.isClicked=true;
     bttnInfo.id=id;
     alert(bttnInfo.isClicked);
     alert(bttnInfo.id);
    	}
    }
    </script>
    </head>
    <body bgcolor="#FFFFFF" text="#000000" link="#0066FF" vlink="#0066FF" alink="#0066FF">
    <form name="frm">
    <input type="button" value="click" id="button1" onclick="clicked(this.id)">
    </form>
    </body>
    </html>

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    perhaps I'm misunderstanding you. Do you mean 'isClicked'?

    place this just before your closing script tag:

    alert(isClicked);

    because you can use isClicked within a function that uses an object does not mean you are breaking the rules -- isClicked is a property of bttnInfo, not a variable.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is this not a variable?

    var isClicked

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you misunderstand, Poop Shoot
    Code:
    function bttnInfo()
    {
       var isClicked, id;
       this.isClicked = false;
       this.is = null;
    }
    In the code above, var isClicked and this.isClicked are separate. Your clicked() function modified the isClicked property, and not the isClicked variable that is local to bttnInfo().

    BTW, you shouldn't create a variable with the same name as a function (bttnInfo = new bttnInfo()) -- as you have now destroyed your bttnInfo() constructor, and will not be able to create another instance.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanxs again beetle I understand completly now!
    Last edited by Poop_Shoot; Sep 8, 2003 at 13:06.

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PSSST! It's two e's, one t. beetle.

    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you know how many times I have done that! And its not like I dont know either I just type hastly sorry!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •