Declaring public variables inside a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sister Ray

    Declaring public variables inside a function

    I've searched for a way to this in a lot of forums. Haven't found a
    solution for this, however I've found a work around.
    Declaring a public object and then adding the variables i want as
    members of thta object, like this

    <script>
    function myobject(){}

    var a = new myobject();

    function foo()
    {
    a.var1 = "some string";
    }

    function foo2()
    {
    //a.var1 exists in foo2
    alert(a.var1);
    }
    //and outside the function
    alert(a.var1);

    this spends a litle more memory but does what i wanted. apart from
    obvious "why would i want to this", does anybody have a better
    solution for this?

    Best Regards.
  • Tom de Neef

    #2
    Re: Declaring public variables inside a function

    "Sister Ray" <carlospedr@gma il.comschreef in bericht
    news:c7924fa7-7474-477e-8a66-8f4a6a132490@59 g2000hsb.google groups.com...
    I've searched for a way to this in a lot of forums. Haven't found a
    solution for this, however I've found a work around.
    Declaring a public object and then adding the variables i want as
    members of thta object, like this
    >
    <script>
    function myobject(){}
    >
    var a = new myobject();
    >
    function foo()
    {
    a.var1 = "some string";
    }
    >
    function foo2()
    {
    //a.var1 exists in foo2
    alert(a.var1);
    }
    //and outside the function
    alert(a.var1);
    >
    I thought that any variable NOT declared will have global scope.
    function foo() {var1 = "some string"}
    will result in var1 as a variable with global scope whose value will be
    assigned when foo() is called.
    You may want to check this.
    Tom



    Comment

    • Dr J R Stockton

      #3
      Re: Declaring public variables inside a function

      On Sep 22, 7:49 pm, "Tom de Neef" <tden...@qolor. nlwrote:
      I thought that any variable NOT declared will have global scope.
       function foo() {var1 = "some string"}
      will result in var1 as a variable with global scope whose value will be
      assigned when foo() is called.
      ISTM that var1 is created the first time foo is called, and I suppose
      assigned every time foo is called. Before foo is first called, var1
      does not exist.

      --
      (c) John Stockton, near London, UK. Posting with Google.
      Mail: J.R.""""""""@ph ysics.org or (better) via Home Page at
      Web: <URL:http://www.merlyn.demo n.co.uk/>
      FAQish topics, acronyms, links, etc.; Date, Delphi,
      JavaScript, .....|

      Comment

      • Gregor Kofler

        #4
        Re: Declaring public variables inside a function

        Sister Ray meinte:
        I've searched for a way to this in a lot of forums. Haven't found a
        solution for this, however I've found a work around.
        Searched for what? It's pretty annoying to make up problem descriptions
        by combining subjects and message texts.

        Just *don't* declare anything - and the variable will end up in the
        global scope. That's perhaps /the/ problem of JS.
        Declaring a public object and then adding the variables i want as
        members of thta object, like this
        >
        <script>
        function myobject(){}
        >
        var a = new myobject();
        Constructors should start with capital letters.

        [useless stuff snipped]

        If you insist:

        <script type="text/javascript">
        var myglobalscope = this;

        function foo() {
        myglobalscope.b ar = 42;
        }

        function foo2() {
        window.alert(my globalscope["bar"]);
        }

        foo();
        foo2();
        </script>

        Gregor


        --
        http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
        http://web.gregorkofler.com ::: meine JS-Spielwiese
        http://www.image2d.com ::: Bildagentur für den alpinen Raum

        Comment

        • Sister Ray

          #5
          Re: Declaring public variables inside a function

          thank's it does work.

          Comment

          Working...