www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Thread: [RESOLVED] Defining variables to an unknown

  1. #1
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792

    resolved [RESOLVED] Defining variables to an unknown

    Hey everyone... I'm just throwing out a question that I would hope could be answered. I don't mind any answer or comments that come of it

    In the code below I've just given an example of what would be nice to achieve. I have created a function (Test), and a function within it (GetMoreValues). When I call test 2 local variables are created (VALUE_1 and VALUE_2). Now what I would like to do is when I call GetMoreValues is to assign more variables to the Test functions namespace, as if I declared them locally along with VALUE_1/2. So the question actually is... what should ??. be? However I do not want any of the variables ending up global.

    Code:
    <script type="text/javascript">
    
    
    	function Test()
    	{
    		var
    			VALUE_1 = 1,
    			VALUE_2 = 2;
    
    		function GetMoreValues()
    		{
    			??.VALUE_3 = 3; //MUST NOT BECOME GLOBAL
    			??.VALUE_4 = 4;
    		}
    		GetMoreValues();
    
    		alert(
    			VALUE_1 + '\n' +
    			VALUE_2 + '\n' +
    			VALUE_3 + '\n' + //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    			VALUE_4          //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    		);
    
    	}
    	Test();
    
    	alert(VALUE_1); //SHOULD BE undefined
    	alert(VALUE_4); //SHOULD BE undefined
    
    </script>

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Not unless you go making objects, or just a namespace variable in the scope you want. But this can't really be accurately answered generally because there is no syntax answer that I know, you'll have to give us more details of why this would be useful.
    Great wit and madness are near allied, and fine a line their bounds divide.

  3. #3
    Join Date
    Dec 2005
    Posts
    2,984
    Your objective seems simple enough to achieve:

    Code:
    	function Test()
    	{
    		var
    			VALUE_1 = 1,
    			VALUE_2 = 2,
                            VALUE_3 = undefined,
                            VALUE_4 = undefined;
    
    		function GetMoreValues()
    		{
    			VALUE_3 = 3; //MUST NOT BECOME GLOBAL
    			VALUE_4 = 4;
    		}
    		GetMoreValues();
    
    		alert(
    			VALUE_1 + '\n' +
    			VALUE_2 + '\n' +
    			VALUE_3 + '\n' + //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    			VALUE_4          //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    		);
    
    	}
    	Test();
    
    	alert(VALUE_1); //SHOULD BE undefined
    	alert(VALUE_4); //SHOULD BE undefined
    Because you have not said var VALUE_3,VALUE_4 inside of the function GetMoreValues(), then they will not be assigned in the scope of that function but will look for the next scope up the chain which is the scope of Test(). JS will see that var VALUE_3,VALUE_4 have been created within this scope so it will assign the values given to these variables inside GetMoreValues() to the variables that exist in the function's closest scope where those variables also exist - the scope of Test().

    A proof of it can be demonstrated by running the following code:

    Code:
    	function Test()
    	{
    		var
    			VALUE_1 = 1,
    			VALUE_2 = 2,
                            VALUE_3 = undefined,
                            VALUE_4 = undefined;
    
    		alert(
    			VALUE_1 + '\n' +
    			VALUE_2 + '\n' +
    			VALUE_3 + '\n' + //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    			VALUE_4          //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    		);						
    						
    		function GetMoreValues()
    		{
    			VALUE_3 = 3; //MUST NOT BECOME GLOBAL
    			VALUE_4 = 4;
    		}
    		GetMoreValues();
    
    		alert(
    			VALUE_1 + '\n' +
    			VALUE_2 + '\n' +
    			VALUE_3 + '\n' + //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    			VALUE_4          //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    		);
    
    	}
    	Test();
    
    	alert(VALUE_1); //SHOULD BE undefined
    	alert(VALUE_4); //SHOULD BE undefined
    Last edited by aj_nsc; 12-12-2011 at 01:59 PM.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Thank you both for you're comments.

    Quote Originally Posted by Declan1991 View Post
    Not unless you go making objects, or just a namespace variable in the scope you want. But this can't really be accurately answered generally because there is no syntax answer that I know, you'll have to give us more details of why this would be useful.
    If this were possible it would be extremely useful to me. My pet project is to build a Web OS of my own (I know there are many already, but this is more of a learning exercise for me). You can see it by going to my home page if anyone is interested in its development.

    If an application or its controls could be requested and insert its own API/Constants into a public area it would make things nice and easy. Right now I'm doing it using objects, but currently each object holds it's own set of values. But it would be nice to have them all suddenly publicly available.

    @aj_nsc, your suggestions are all valid, but the values in my example are all undertermined until code is downloaded and processed into the system.

    The reason for not using global variables is because the application can be instanced seperately and each with its own memory space.

    Thanks again for responding

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    a function is an object, and objects are namespaces, which are lexically scoped via with:
    Code:
    	function Test()
    	{ with(Test){
    		var  VALUE_1 = 1,
    			VALUE_2 = 2;
    
    		function GetMoreValues(name, value)
    		{
    		   Test.VALUE_3=3;
    		   Test.VALUE_4=4;
    		}
    		GetMoreValues();
    
    
    		alert(
    			VALUE_1 + '\n' +
    			VALUE_2 + '\n' +
    			VALUE_3 + '\n' +
    			VALUE_4         
    		);
    
    	}}
    	Test();
    
    	alert(VALUE_1); //SHOULD BE undefined
    	alert(VALUE_4); //SHOULD BE undefined
    purists may grumble, and performance might take a slight hit, but with() provides the capability described, works in all browsers, and uses a cleaner syntax (11 chars!) than objects or eval hacks.
    Last edited by rnd me; 12-12-2011 at 06:09 PM.

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    OMG...

    That is the most brilliant thing anyone has said to me in a long while!
    I've tried avoiding "with" for so long I didn't even think to consider it.

    I like that purists may grumble comment as well
    I'll probably use what you've suggested just above my eval statement anyways, mwahaha!

    Thank you again, you're awesome.

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by rnd me View Post
    purists may grumble, and performance might take a slight hit, but with() provides the capability described, works in all browsers, and uses a cleaner syntax (11 chars!) than objects or eval hacks.
    That's funny, I haven't seen with in so long, I never thought of it! I was trying to come up with a way to avoid using "Test." in front of everything, and couldn't!
    Great wit and madness are near allied, and fine a line their bounds divide.

  8. #8
    Join Date
    Dec 2005
    Posts
    2,984
    Is there something wrong with creating an empty object inside of test that could hold your 'unknown' properties:

    Code:
    	function Test()
    	{
    		var EXTENSIBLE = {
                              VALUE_1 : 1,
                              VALUE_2 : 2
                          };
    
    		function GetMoreValues()
    		{
    			EXTENSIBLE.VALUE_3 = 3; //MUST NOT BECOME GLOBAL
    			EXTENSIBLE.VALUE_4 = 4;
    		}
    		GetMoreValues();
    
    		alert(
    			EXTENSIBLE.VALUE_1 + '\n' +
    			EXTENSIBLE.VALUE_2 + '\n' +
    			EXTENSIBLE.VALUE_3 + '\n' + //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    			EXTENSIBLE.VALUE_4          //SOMEHOW ASSIGNED INSIDE Test USING GetMoreValues
    		);
    
    	}
    	Test();
    
    	alert(VALUE_1); //SHOULD BE undefined
    	alert(VALUE_4); //SHOULD BE undefined
    If you've got a complex app, playing with the 'with' structure this could give you a serious performance hit.

    I'm still not quite sure what you're ultimate goal is other than 'not create globals' (which could be accomplished about a thousand ways).
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  9. #9
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Alright, finally managed to do some testing with the "with" keyword. As expected it works really well to simulate a global scope and share variable values between pieces of external code.

    But... Google Chrome reports errors that "with" is not allowed to be used when the script is in strict mode (which I've recently adopted as well). Every other browse doesn't seem to mind running it though. Obviously if I take off strict mode it runs everywhere, but I don't really want to.

    I've also read that "with" is either deprecated or being deprecated.

    @aj_nsc, I'm interested to know the many ways that this could be done alternatively.

    The thoughts I had were, as you mentioned, creating an "EXTENSIBLE" type of variable and reading/writing from it directly. Or using a function like E('VALUE') for it to retrieve a value from some hidden array. And at the moment Im using each object type I create to house the public values they are sharing.

    Basically, all I need this functionality for it to make "constants" public to other parts of the code, I know constants don't really exist but I want to fake it anyway. So I could run flag based commands like so:

    Code:
    CreateSomething(HAS_EVENT | DOES_THIS | LOOKS_GOOD);
    instead of

    Code:
    CreateSomething(MyObject.HAS_EVENT | MyObject.DOES_THIS | MyObject.LOOKS_GOOD);
    Thanks everyone for your comments so far.

  10. #10
    Join Date
    Dec 2005
    Posts
    2,984
    Have you ever though about doing something with object literal notation?

    Code:
    var MyAppNameSpace = {
    
              VALUE_1 : 1,         
              VALUE_2 : 2,
    
    
               init : function() {
                   //do some initiating things
               },
    
    
               createConstant : function(name,val) {
                    this[name] = val;
               },
    
               localsOnly : function() {
                   var VALUE_1,VALUE_2;
                   alert(VALUE_1); //undefined
               },
    
               constantsOnly : function() {
                   alert(this['VALUE_1']); //1
               }
    
    
    };
    
    //creates A_CONSTANT accessible by MyAppNameSpace.A_CONSTANT;
    MyAppNameSpace.createConstant('A_CONSTANT',10);
    Still utterly confused on what you're trying to do. I think I'm so confused because you tried to do it (whatever 'it' is, I don't know) inside of a single function rather than instantiating a new object with that function as a constructor, or using object literal notation.

    Have you explored the new keyword, or using 'namespaces' provided by object literals? (Just in case - object literals are for singleton apps while the 'new' keyword is for apps which will have multiple instances at the same time).
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    @aj_nsc, thanks again for your suggestions.

    I will admit, what I am doing is not straight forward.

    What you put forward using object literal notation is the equivalent of just using the EXTENSIBLE variable in the previous post, except there are functions regulating things. What you end up with is having to use the objects name as a prefix in order to access its members. eg. MyAppNameSpace.A_CONSTANT;

    Just to make things more complicated, I'm going to provide a basic example of what I'm doing...

    Code:
    <script type="text/javascript">
    
    	function App()
    	{
    		var System = {Globals : {}, Functions : {}};
    
    		function RunFunction(name)
    		{
    			System.Functions[name]();
    		}
    		function Compile(name, code)
    		{
    			var Global = System.Globals;
    			with (Global) {System.Functions[name] = eval('(function(Me){' + code + 'return Me;}());');}
    		}
    		Compile('function1', 'Global.VALUE_1 = 1, Global.VALUE_2 = 2; Me = function(x) {alert("FUNCTION 1: " + VALUE_1 + " " + VALUE_2 + " " + VALUE_3 + " " + VALUE_4 + " " + x);};');
    		Compile('function2', 'Global.VALUE_3 = 3, Global.VALUE_4 = 4; Me = function(x) {alert("FUNCTION 2: " + VALUE_1 + " " + VALUE_2 + " " + VALUE_3 + " " + VALUE_4 + " " + x);};');
    
    		with (System.Functions) {
    			function1('First');
    			function2('Second');
    		}
    	}
    	App();
    
    </script>
    So basically, each evaluated function defined its own set of "constants" (Global.VALUE_x), but when called they are able to see each others values (VALUE_x), without any reference to external objects in the alert boxes.

    Purists, eat your heart out

  12. #12
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I, for one, am not so interested in your code as I am the effin point.

    What's the point? What the devil are you trying to build?

    Whatever it is, I can almost assure you, it's easier than you're making it.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  13. #13
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Quote Originally Posted by svidgen View Post
    I, for one, am not so interested in your code as I am the effin point.

    What's the point? What the devil are you trying to build?

    Whatever it is, I can almost assure you, it's easier than you're making it.
    Lol, that was funny. I'm going to enable debug mode on my website so you can see what I'm dealing with right now (scroll down the page). http://www.bionoid.com/
    The code is currently as easy as I can make it.

  14. #14
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Very pretty. Still not seeing the point though.

    I mean, you're obviously trying to build a Web OS of some sort. But, the capabilities of that "OS" aren't clear. Are you trying to facilitate some security between ... things? IE, allow a 3rd party developer to create an "application" that cannot/will not interfere with the functioning of another application? (Or the "OS" itself?)

    What's the grand vision for this "OS"?
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  15. #15
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Quote Originally Posted by svidgen View Post
    Very pretty. Still not seeing the point though.

    I mean, you're obviously trying to build a Web OS of some sort. But, the capabilities of that "OS" aren't clear. Are you trying to facilitate some security between ... things? IE, allow a 3rd party developer to create an "application" that cannot/will not interfere with the functioning of another application? (Or the "OS" itself?)

    What's the grand vision for this "OS"?
    In regards to the question I've been asking, the point was to make data easier to find, like constants, they are normally accessible anywhere in a project without object references. Thats all I was asking for... my project otherwise works just fine.

    I'm still laying out foundation for it so things like scoped constants, objects events, internal messenging, themes etc, things that would be nice to have sorted out before writing applications for it and having to change everything cause you found a better way. So I'm exploring my options.

    The grand vision? Honestly, I will most likely build all my past projects into the OS once it's done. You know the kind, the projects you started but left before finishing them to gather dust.

    In the meantime, I've learnt tons of Javascript and I'm having fun with it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles