Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38

Thread: if(variable)

  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by ricmetal View Post
    okay, so i CANT pass an undefined variable to a function and have it (the function) check if it (the variable) is defined in the global scope...

    is there any way around this? it feels like javascript is failing short to provide us a little bit of flexibility in that matter
    I really don't know why you say that. I feel it is your code that has the shortcomings, that you ever need to check to see if some global variable has been declared or not.

    Remember, it *DOES* work if you have at least done var whatever;

    Why would you ever need to test if you forgot to do var whatever;??? When in doubt, just add the var declaration. It won't hurt.

    If you are writing a library and want to test to see if the library itself has already been loaded, then just look at how many many libraries do this. It's a bit of a hack but it works:
    Code:
    whatever = whatever || new foobar( );
    So if whatever is defined, that just assigns it to itself; a no-op. If not, then it creates an instance of whatever it is you need.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    yeah, im not creating a library. there might be more complex inner workings to javascript but i would think they would have tied up two pretty basic things: hey, lets let a function..check if a variable is declared! i mean, why not. anyways, you mention that libraries check for undefined variables using a little hack. more of a more reason to have an easy way to do this, and why not from inside a function. i mean, they are there to create reusable code right? anyways..i was just trying to figure out how to create a function that would check some falsy values i wanted to check. in those, undefined was included.cheers
    Last edited by ricmetal; 08-09-2013 at 07:10 PM.

  3. #18
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Look at it this way: Almost no other language would even let you get away without explicitly declaring all variables ahead of time. At least JavaScript *does* allow the kind of hacks I showed.

    I guess I'm not sure what you would have JavaScript do in the case where you call
    Code:
    if isDefined( somename ) ...
    and somename is not even declared.

    JavaScript has to push *SOMETHING* onto the stack when it makes the call to the function. What *CAN* it push on the stack in the case where that something has not even been declared??

    The only option I can see is that we could ask JavaScript to then automagically declare the variable for us. Which of course would certainly be possible. But then there's no difference between calling with an undeclared variable and calling with a declared one. Defeating the purpose of testing for undeclared variables.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. #19
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    the function could let us know the variable was not defined. plain and simple.

    it already does that by throwing a reference error.
    plus we can test this outside of a function so i dont see why javascript would limit the function.
    sure, i can change the way i write my program. but it would sure be a helpful addition to functions afaik.

    especially when we use nearly nothing BUT variables when dealing with functions...

    there might be a reason for 'limiting' the function, but then again, maybe not

    regarding automagically declaring variables i dont think that would play nice. i would not like that- plus, yeah, it would counter our attempt to find if a varibale is undefined.
    Last edited by ricmetal; 08-09-2013 at 08:50 PM.

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I think you are not aware of the difficulty of compiling the language you seem to be wanting. And make no mistakes: JavaScript *IS* a compiled language. It might pretend to be an interpreted language, but no true interpreter could ever give you the features and (especially) the performance that JavaScript gives you.

    You would be asking JavaScript to compile something that doesn't exist. And so its only choice would, indeed, be to create a "placeholder" for your non-existent variable. And guess what that "placeholder" would then be the equivalent of? Yep, you got it. A declared variable.

    The reason you get the error when you try to call someFunction( somename ) when somename has not been declared is that, indeed, JavaScript has no idea what you might REALLY mean. How can it tell the difference, for example, between someFunction( This ) (a simple misspelling of "this") and someFunction( foay18sasx881x ) (a nonsense name). To it, they are both just gobbledy-gook and so are an error.

    I think you would soon find that having JavaScript automatically assume that This is an as-yet-undeclared variable would lead to no end of problems.

    As a practical matter, for example, what would be the difference between that assumption and the assumption that document.getElementByeByeId("xyz") is simply an as-yet-undeclared function, meaning that JS should somehow create a skeleton undeclared function for you? For that matter, how would JS know that This is supposed to be an undeclared variable name and not an undeclared function?

    Tell you what. You invent your own language and see if you can get it to do all this magic you want JS to do. Me, I'm almost overwhelmed with what JS does, already.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Quote Originally Posted by ricmetal View Post
    is there any way around this? it feels like javascript is failing short to provide us a little bit of flexibility in that matter
    Already answered

    Quote Originally Posted by Old Pedant View Post
    If you want to test *THAT* then *DO NOT PASS THE VARIABLE* to the function:
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  7. Users who have thanked felgall for this post:

    Old Pedant (08-09-2013)

  8. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yeah, and on top of that, I showed how he could do it by passing the *name* of the variable to test as a string.

    How many ways around things does he want?

    But I do understand what he is asking. I just don't know how I would construct a reasonable compiler that could handle it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #23
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    that answer is not really an answer to my problem, as the function's purpose would be to test any variable i pass through it.

    how about a hack like this, would this be raised in any other situation except passing an undefined variable to the function?

    Code:
    function newMe(a){
    	return a in window;
    }
    
    try {
    	newMe(a); // will rise a ReferenceError exception
    } catch( ex ) {
    	 if( ex.constructor == ReferenceError ) {
    		alert('function called undefined variable');
    	 }
    }
    Last edited by ricmetal; 08-09-2013 at 11:40 PM.

  10. #24
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    If you are writing modern JavaScript then you MUST declare all variables. The script will simply refuse to run if a variable is not declared. So having variables not declared is only an issue with historical JavaScript.

    To use modern JavaScript place the following statement at the top of your code: "use strict";
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  11. #25
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    noted. thanks.

    this undefined check was not meant for me as a developer. i am aware of the variables i create.
    i was aiming at people that will use the software i write (whom need to correctly set up some configuration variables [before installing] for the software).

    i am trying to automate as much as possible to have less trouble trouble-shooting installation problems. therefore my extensive attempt.

    i am realizing however that the more i try and make sure everything is set-up properly by automatizing everything, the more specific code i have to write, and the more problems come from this.

    i will be leaving some checks aside from the code and have them in a check list for troubleshooting installation problems. because the problems should only occur in simple situations like accidental deleting a config variable..

    EDIT:
    basally, i quit trying to use a function to check for undefined variables.
    not even with the try catch method because i'd want to have a function to pass the variable to, and do the try catch, but of course i cannot get inside the function if the variables is not declared so i quit.

    for now

  12. #26
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    A much preferable way to require predefined values (as settings, for example, is to require an *OBJECT* be passed to your code.

    Example:
    Code:
    function setUp( setUpObj )
    {
        if ( setUpObj.zamboni )
        {
           ... initialized the zamboni-related stuff ...
        }
        ... and similar ..
    }
    And now the user need only create one object, and you don't care if they create it as a variable or not. That is, you don't care if they do
    Code:
    var foo = {
         zamboni: 3.14159265,
         rowrbazzle: "hello"
    };
    setUp(foo);
    or do it all inline:
    Code:
    setUp( { zamboni: 37, nukeit: true, exeunt: "all" } );
    And you don't care if they pollute their space with a global or not.

    Further, I'd like to point out that in your original quest, above, you can't *know* that they will be using any globals, at all, when calling you. They might be calling your code from inside their own master anonymous function. If you require them to set up particular globals, by name, that's the weakest of all schemes I can think of.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #27
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Further, I'd like to point out that in your original quest, above, you can't *know* that they will be using any globals, at all, when calling you. They might be calling your code from inside their own master anonymous function. If you require them to set up particular globals, by name, that's the weakest of all schemes I can think of.
    okay, i realize that, but that sounds more like if i was making a plugin of some sort. the software i have is a full installation, like an entire app, so i shouldn't be worried about to much customizations by users, nor calls from elsewhere.

    the more i look into the way i've built this app and the more i read and see how other apps are built, the more i realize that im far from best architectural practices and believe me it hurts not to see my code written like books on a shelf but hey - ive tested, debugged and adhered to alot of good practices, otherwise.

    anyways, the whole undefined thing still sticks as a way of doing things, to bad i couldnt get it to work.

    then again, there's only so much checks we can do. this started out to check if the user populated the config variables correctly and went down to checking for undefined first, but yeah, im realizing theres only so much checking that can be done before it becomes absurd hehe, plus the undefined check was a dead end but thats good cause it made me realize the absurdity of too much checking for errors. i mean, if(this.if exists) hehe
    Last edited by ricmetal; 08-10-2013 at 02:23 AM.

  14. #28
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    If you wrap your entire code in an anonymous function and specify all the values that the user may need to change as values being passed into the function then they only need to edit the values in the part of the function that triggers running the code that appears at the very bottom of the code. See http://www.felgall.com/jsdist.htm for an example where an id and an array are the two variables that the user can change.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  15. #29
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If you were writing that again, today, would you perhaps consider invoking the function more like this?
    Code:
    ( { where: 'f',
         cities: {
            'Paris' : [],
            'Madrid' : [1268],
            'Lisbon' : [1786,638],
            'Milan' : [850,1730,2368]
         }
    );
    ???

    Or would you stick with the multi-level array?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  16. #30
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    If you wrap your entire code in an anonymous function and specify all the values that the user may need to change as values being passed into the function then they only need to edit the values in the part of the function that triggers running the code that appears at the very bottom of the code. See http://www.felgall.com/jsdist.htm for an example where an id and an array are the two variables that the user can change.
    great, but that doesnt differ much from what ive got. the user can still mess up when changing the variable's data, like accidentaly deleting a row, and the code breaks. this is why i wanted to test for undefined..
    Last edited by ricmetal; 08-10-2013 at 04:48 AM.


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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