-
Sep 17th, 2018, 03:19 PM
#1
Thread Starter
PowerPoster
[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,
-
Sep 17th, 2018, 07:35 PM
#2
Re: Not sure what this statement is doing?
Ternary operator.
is shorthand for
Code:
if x = True Then
w = y
else
w = z
end if
-
Sep 17th, 2018, 08:03 PM
#3
Thread Starter
PowerPoster
Re: Not sure what this statement is doing?
I tried looking up that operator and could not find it anywhere....Thank you!
-
Sep 17th, 2018, 08:31 PM
#4
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:
var doc = ifrm.contentDocument != null ? ifrm.contentDocument : ifrm.contentWindow.document;
In C#, that would be beter written like this:
csharp Code:
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:
Dim doc = If(ifrm.contentDocument IsNot Nothing, ifrm.contentDocument, ifrm.contentWindow.document)
and:
vb.net Code:
Dim doc = If(ifrm.contentDocument, ifrm.contentWindow.document)
-
Sep 18th, 2018, 09:02 AM
#5
Thread Starter
PowerPoster
Re: Not sure what this statement is doing?
-
Sep 18th, 2018, 01:25 PM
#6
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;
-
Sep 18th, 2018, 06:36 PM
#7
Re: Not sure what this statement is doing?
Originally Posted by szlamany
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'.
-
Sep 18th, 2018, 06:48 PM
#8
Thread Starter
PowerPoster
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|