Web Analytics Made Easy -
StatCounter Variable Scoping help/issues! - CodingForum

Announcement

Collapse
No announcement yet.

Variable Scoping help/issues!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Variable Scoping help/issues!

    Hi all,

    I have the following simple complete javascript example that all I am doing is instantiating an "object", and then trying to access a variable. I keep getting "undefined" errors...I know I'm missing the obvious!

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    
    function myClass() 
    {
        var EVENT_CONFIRM_EVENT = 4;               
    }
    
    function init()
    {
        obj=new myClass();
        alert("obj ["+obj+"]\nEvent ["+obj.EVENT_CONFIRM_EVENT+"]");
    // The error is here-------------^^^^^^^^^^^^^^^^^^
    }
    
    </script>
    </head>
    
    <body onload="init();">
    
    </body>
    </html>
    Any thoughts?!

    ~Kam (^8*

  • #2
    A variable declared (using var) does *NOT* become a member of the class.

    You create a member of the class using this.

    There's a good reason for this. Here is a stupidly contrived example, but I think it illustrates the point:

    Code:
    <script>
    function myClass( )
    {
        var FOO = 2;
        this.EVENT_CONFIRM_EVENT = FOO * 2;
    }
    ...
    See? You might have reason to create a variable that has scope only inside the constructor function. You wouldn't want it to just automatically become a member of the class.

    And then you create the class member by assigning it as a member of "this", which of course is the object being constructed.

    Make sense?

    In, say, Java code, you would do the equivalent thus:
    Code:
    class myClass
    {
        private int EVENT_CONFIRM_EVENT;
        myClass( ) 
        {
            int foo = 2;
            EVENT_CONFIRM_EVENT = foo * 2;
        }
    }
    But because JS doesn't have that syntax, it just uses assignment to this to establish class members.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      if you remove the var from myClass, it's global then and you can get the value. otherwise it's a local value
      Code:
      <html>
      <head>
      
      <script language="JavaScript">
      
      
      
      function init()
      { 
       var myClass=function() {return 4;}();
      
          obj= myClass;
          alert(obj);
      tryagain();
      }
      
      function tryagain(){alert('hello');alert(myClass);alert(obj)}
      
      </script>
      </head>
      
      <body onload="init();">
      
      </body>
      </html>

      Comment


      • #4
        Well, yeah...but that's not what he wanted. He *wants* it to be a member of the class/object.

        Look at how he uses it:
        Code:
        obj.EVENT_CONFIRM_EVENT
        That is, he intends for it to be a property of the constructed object.

        If you just remove the var and make it global, then you can't have different values for the property in different instances of the class.
        Be yourself. No one else is as qualified.

        Comment


        • #5
          Originally posted by Old Pedant View Post
          Well, yeah...but that's not what he wanted. He *wants* it to be a member of the class/object.

          Look at how he uses it:
          Code:
          obj.EVENT_CONFIRM_EVENT
          That is, he intends for it to be a property of the constructed object.

          If you just remove the var and make it global, then you can't have different values for the property in different instances of the class.
          I see. can't you just set attribute? Am I missing somthing? I guess it's one more thing I don't have a clue about. Can you refer me to an example of what he's doing?

          Comment


          • #6
            http://www.google.com/search?hl=en&q...ascript+constr

            The very first hit there doesn't do a bad job of explaining it.
            Be yourself. No one else is as qualified.

            Comment

            Working...
            X