SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript - Where The F*** is isset() and is_array() ?!

    I'm honestly tearing out what little hair I have left at the moment. I can't seem to write a script or find a script that can perform an isset() or is_array() with out throwing Javascript errors in Firefox. I just keep thinking... surely someone out there must know how to do this. How can modern Javascript not have this kind of functionality built in.

    Here's the angle I'm coming from.

    Code:
    function is_array(variable)
    {
      
      if( typeof(variable) == 'undefined' )
      {
      
        return false;
        
      } else {
        
        //If this variable is an object?
        if ( typeof(variable) == 'object' )
        {
          
          if(variable.length)
          {
            
            return true;
            
          } else { //The length was zero or null
            
            return false;
            
          }
          
        } else { //It was not an object
          
          return false;
          
        }
        
      }
      
    }
    if(is_array(nothing))
    {
      
      alert('Yes - an array');
      
    } else {
      
      alert('Not - an array');
      
    }
    Instead of getting "Yes - an array" or "Not - an array" I get some errors in the Firefox error console.

    Error: nothing is not defined
    Line: 89
    And

    Error: text is null
    Line: 117

  2. #2
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    which is line 89 in your code?

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    The problem here is that you're trying to apply what you know from PHP on to JavaScript. Things don't work well that way.

    Write in the language that you're writing in, is the maxim.

    In this case, Array.isArray exists in JavaScript 1.8.5 and onwards.
    You would do well though to investigate the compatibility code from the Array.isArray documentation page, so that you can use the same technique in browsers that don't yet support JavaScript 1.8.5
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The problem here is that you're trying to apply what you know from PHP on to JavaScript. Things don't work well that way.

    Write in the language that you're writing in, is the maxim.

    In this case, Array.isArray exists in JavaScript 1.8.5 and onwards.
    You would do well though to investigate the compatibility code from the Array.isArray so that you can use the same technique in browsers that don't yet support JavaScript 1.8.5
    OMG. You are so right. Thank you x1000

  5. #5
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Max Height View Post
    which is line 89 in your code?
    if(is_array(nothing))

  6. #6
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wh33t View Post
    Code:
    Error: nothing is not defined
    Line: 89
    That error on line 89 is if (is_array(nothing))

    It happens because you are trying to use the var "nothing" but it isn't defined anywhere.
    Try
    Code:
    var nothing = undefined;
    if(is_array(nothing)) {
    //etc...
    Or simply calling it without a parameter will render variable as undefined.

    Like Paul said, you're trying to apply some PHP principles here, it might be worth checking out PHP JS which is an attempt to port a lot of PHP functions to JavaScript :-)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  7. #7
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The problem here is that you're trying to apply what you know from PHP on to JavaScript. Things don't work well that way.

    Write in the language that you're writing in, is the maxim.

    In this case, Array.isArray exists in JavaScript 1.8.5 and onwards.
    You would do well though to investigate the compatibility code from the Array.isArray documentation page, so that you can use the same technique in browsers that don't yet support JavaScript 1.8.5
    Ok, so perhaps I'm still doing that PHP as JS thing.

    Here is the issue now

    Code:
    if(!Array.isArray) {  
      Array.isArray = function (arg) {  
        return Object.prototype.toString.call(arg) == '[object Array]';  
      };  
    } 
    
    if(Array.isArray(nothing))
    {
      
      alert('Yes - an array');
      
    } else {
      
      alert('Not - an array');
      
    }
    Errors:

    Error: nothing is not defined
    Line: 60
    Line: 60

    Code:
    if(Array.isArray(nothing))

  8. #8
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Oh, additionally, SugarJS implements some nice extensions on native objects that I've found useful on occasion. (And they implement isArray on Arrays too)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by wh33t View Post
    Error: nothing is not defined
    Line: 60
    [/code]
    That's right. You're being told that the nothing variable doesn't exist, giving you an opportunity to fix that error.

    If you want to keep the undefined variable in there, though who knows why, you could check to see that it's defined first before using it.

    Code javascript:
    if (typeof nothing !== 'undefined') {
        // do stuff with nothing
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    That's right. You're being told that the nothing variable doesn't exist, giving you an opportunity to fix that error.

    If you want to keep the undefined variable in there, though who knows why, you could check to see that it's defined first before using it.

    Code javascript:
    if (typeof nothing !== 'undefined') {
        // do stuff with nothing
    }
    Yea, I get that now. I'm trying to emulate how PHP does this. In PHP with most versions you simply just go is_array(something). If it is an array you get true in return, if it's entirely "anything" else you get false, which is what I was expecting I could do with JS. I'm just gotta rethink my strategy here.

  11. #11
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Funny that no one has mentioned javascript's typeof operator yet.

  12. #12
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wh33t View Post
    Yea, I get that now. I'm trying to emulate how PHP does this. In PHP with most versions you simply just go is_array(something). If it is an array you get true in return, if it's entirely "anything" else you get false, which is what I was expecting I could do with JS. I'm just gotta rethink my strategy here.
    All you really need is for nothing to be defined before you use it. As a general rule you would declare any variables before you use them first, so you would avoid the reference error.

    i.e.
    Code javascript:
    var nothing = null;
    var something = [1, 2, 3];
     
    Array.isArray(someUndefinedVariable) // Will fail, 'someUndefinedVariable' hasn't been defined yet.
     
    Array.isArray(nothing); // This will work and return false.
     
    Array.isArray(something); // This will work and return true.


    Quote Originally Posted by Michael Morris View Post
    Funny that no one has mentioned javascript's typeof operator yet.
    Well, the OP does use typeof in his code

    Alas, typeof will just say that an array is of the object type.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Funny that no one has mentioned javascript's typeof operator yet.
    I do in post #9 when it comes to checking for an undefined variable, but typeof is not all that useful when it comes to checking for an array.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Serves me right for skimming eh?

    Oh well. Sorry.


Bookmarks

Posting Permissions

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