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 3 of 3 FirstFirst 123
Results 31 to 38 of 38

Thread: if(variable)

  1. #31
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,460
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by Old Pedant View Post
    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?
    The cities are ordered in the sense that the distances in the associated arrays refer to the previously listed cities in the order that they are listed. Switching to using an object would remove that built in ordering between the cities and so it would no longer be as obvious which city is 1268km from Madrid and which cities are 1786km and 638km from Lisbon etc. and not as easy to retrieve the correct distance.

    If I was switching to using objects I'd pair the cities and use a structure with two city names and a distance - although that arrangement would make it less obvious as to whether all the necessary pairs had been included.

    So I think I'd stick with the multi-level array where the structure defines which distance applies to which two cities.
    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.

  2. #32
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Makes sense. As a practical matter, if you run an iterator on the cities they would appear in the order you entered them, but I agree that you shouldn't count on things like that.
    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.

  3. #33
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    okay, so i was not understanding that i should pass the variable name as a string, because after all i was testing a variable

    PHP Code:
    function isUndeclared(a) {
        if(
    a in window) { // aparently its this that resolves itself as [I]"a" in window[/I] and gets the code to work
            
    alert('Declared');
        } else {
            
    alert('Undeclared');
        }
    }
    // var a = "good";
    isUndeclared("a"); // Called as a string. alerts Undeclared 
    Last edited by ricmetal; 08-11-2013 at 02:53 AM.

  4. #34
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by ricmetal View Post
    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!

    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
    you can do all those things with the built in javascript tools.


    if you have a reference to the var you want to check, use
    Code:
    myVar!==undefined
    if you have the name of a var you want to check, use
    Code:
    eval(strName)!==undefined
    if you want to check to see if the name of a global has been declared, use
    Code:
    self.hasOwnProperty(strName)
    if you want to find out if a global has been defined, use
    Code:
     self[strName]!==undefined
    if you want to know if a globally-available symbol is declared, including non-own-self props like __proto__, use
    Code:
    strName in self
    if you need to safely check a var that might not be defined, use the typeof initializer to prevent an undeclared symbol from throwing a reference error.

    I am not sure what scenario javascript leaves out, but it has plenty of tools besides failing to compile to handle types, and i've never had major problems with the fail early/late balance it strikes.
    Last edited by rnd me; 08-11-2013 at 11:24 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  5. #35
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the references rnd me

    but the problem was passing the undefined variable as a function argument, as a variable, instead of referencing it name. it seems functions wont run if we pass a variable to it that has not been declared.

    regards

  6. #36
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,460
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Modern JavaScript only runs if all variables are declared. The browser's built in debugging tools will tell which declaration is missing if the script fails to run because of an undeclared variable.

    It is only possible to test for undeclared variables in historical JavaScript where variables referenced without having previously been declared would pollute the global namespace.
    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. #37
    New Coder
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    what do you mean by modern javascript? is it just the fact that you place use strict at the top of the code that makes it modern or will this be a requirement in the future?

  8. #38
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,460
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by ricmetal View Post
    what do you mean by modern javascript? is it just the fact that you place use strict at the top of the code that makes it modern or will this be a requirement in the future?
    The "use strict"; is what turns on the latest version of JavaScript in the browser in the same way the doctype tag turns on the standard version for HTML. That isn't the only difference though - there are dozens of other changes that have been made to JavaScript since the version used with the Netscape browser that most scripts you see are written for.

    The next version of JavaScript due out later this year will introduce further enhancements to JavaScript that will rely on that command being present.
    Last edited by felgall; 08-12-2013 at 07:46 AM.
    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.


 
Page 3 of 3 FirstFirst 123

Posting Permissions

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