Results 1 to 8 of 8

Thread: [RESOLVED] Not sure what this statement is doing?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Not sure what this statement is doing?

    Can someone please tell me what this statement is saying? Specifically what the ? means...

    Code:
    var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
    Thanks,
    Blake

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Not sure what this statement is doing?

    Ternary operator.

    Code:
    w = x ? y : z
    is shorthand for

    Code:
    if x = True Then
      w = y
    else
      w = z
    end if

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Not sure what this statement is doing?

    I tried looking up that operator and could not find it anywhere....Thank you!
    Blake

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Not sure what this statement is doing?

    One thing to note about that code is it is treating an object like a Boolean value. That is basically the way C and many C-based languages test for null. In a stricter language like C#, you would have to do this:
    csharp Code:
    1. var doc = ifrm.contentDocument != null ? ifrm.contentDocument : ifrm.contentWindow.document;
    In C#, that would be beter written like this:
    csharp Code:
    1. var doc = ifrm.contentDocument ?? ifrm.contentWindow.document;
    I'm not sure whether JavaScript has a null-coalescing operator or not but, if it does, I imagine that it's a fairly new addition that may not be universally supported. For the record, the VB equivalents of those C# expressions are:
    vb.net Code:
    1. Dim doc = If(ifrm.contentDocument IsNot Nothing, ifrm.contentDocument, ifrm.contentWindow.document)
    and:
    vb.net Code:
    1. Dim doc = If(ifrm.contentDocument, ifrm.contentWindow.document)

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Not sure what this statement is doing?

    Thanks jmc...
    Blake

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Not sure what this statement is doing?

    That is odd code - I do that a completely different way

    Code:
    var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
    I would do as

    Code:
    var doc = ifrm.contentDocument || ifrm.contentWindow.document;
    That OR ELSE operator (the ||) will only return the RIGHT SIDE if the LEFT SIDE is not nothing or undefined or null (important point - basically all those "values" are FALSE - all other values are TRUE).

    Look at this code below - it is very common for me.

    Code:
    var g_isMobile = (g_sitesettings.isMobileTest || false);
    For "backwards" compatibility with older installations my g_sitesettings object might be MISSING the isMobileTest key/value pair. If it is then I want FALSE to be returned.

    more examples

    Code:
    var strExtra = objWebParam.objGridHeading || "";
    
    objReturn.login = objReturn.login || false;
    objReturn.button = objReturn.button || false;
    objReturn.dashboard = objReturn.dashboard || false;
    objReturn.acssproc = objReturn.acssproc || false;
    objReturn.acsprint = objReturn.acsprint || false;
    objReturn.acsinfo = objReturn.acsinfo || false;
    objReturn.clrinsproc = objReturn.clrinsproc || false;
    objReturn.username = objReturn.username || false;
    objReturn.formula = objReturn.formula || false;
    objReturn.reportstatus = objReturn.reportstatus || false;
    objReturn.excel = objReturn.excel || false;
    //objReturn.editpanels = objReturn.editpanels || false;
    objReturn.filter = objReturn.filter || false;
    objReturn.txt = objReturn.txt || false;
    objReturn.pdf = objReturn.pdf || false;
    objReturn.pdfexcel = objReturn.pdfexcel || false;
    objReturn.pdfdownload = objReturn.pdfdownload || false;
    objReturn.acscheck = objReturn.acscheck || false;
    objReturn.retainuser = objReturn.retainuser || false;
    objReturn.updatealert = objReturn.updatealert || false;
    objReturn.application = objReturn.application || false;
    objReturn.autodocument = objReturn.autodocument || false;
    objReturn.email = objReturn.email || false;

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Not sure what this statement is doing?

    Quote Originally Posted by szlamany View Post
    That is odd code - I do that a completely different way

    Code:
    var doc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
    I would do as

    Code:
    var doc = ifrm.contentDocument || ifrm.contentWindow.document;
    That OR ELSE operator (the ||) will only return the RIGHT SIDE if the LEFT SIDE is not nothing or undefined or null (important point - basically all those "values" are FALSE - all other values are TRUE).
    So that's basically the null-coalescing operator then. It just doesn't need to be explicit because, as you say, any expression that evaluates to null can be implicitly converted to 'false'.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [RESOLVED] Not sure what this statement is doing?

    Thanks for that clarification. That's one I'll have use in order to get use to it!
    Blake

Posting Permissions

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



Click Here to Expand Forum to Full Width