-
Sep 22nd, 2013, 05:22 PM
#1
VS when "debugging" Javascript does not like this technique
I've been using tricks like this:
Code:
var strId = wesAWC.attr("id") || "";
Basically OR'ing something that might come back as UNDEFINED with "" (in this example) - so that the strId variable gets "set" to blank instead.
Like this also
Code:
var blnFullReMakeOnSave = (wesDDR.data("awcoptions") || {}).fullremakeonsave || false;
I cannot guarantee that "awcoptions" will come back - so I "OR" it with an EMPTY OBJECT {} - and then OR .fullremakeonsave with FALSE in case it's not found in the OBJECT as well.
At any rate - I bought a new PC and when I first hopped into VS it used IE as the default browser - and was debugging the code right in VS (I use FF with FireBug usually).
The IE-debugger was breaking at all these spots telling me I that the "value" was undefined - which is the whole point of why I was OR'ing it with something EMPTY/BLANK.
So - is this a bad technique I've picked up - or is the IE debugger stricter then it needs to be??
-
Sep 22nd, 2013, 07:10 PM
#2
Re: VS when "debugging" Javascript does not like this technique
I see nothing wrong with that as long as wesDDR is defined and wesDDR.data exists as a function. Otherwise, you will get type errors if they don't exist.
Just tried it in the Firefox and Chromium consoles, and no problems there getting "false".
JavaScript Code:
var wesDDR = {}; wesDDR.data = function() {}; var blnFullReMakeOnSave = (wesDDR.data("awcoptions") || {}).fullremakeonsave || false; // blnFullReMakeOnSave is now false
-
Sep 22nd, 2013, 07:15 PM
#3
Re: VS when "debugging" Javascript does not like this technique
I use that syntax all the time to create closures, and I've not seen any problems in IE8+.
E.g.
JavaScript Code:
(function (wesDDR, $, undefined) {
wesDDR.data = function () {
};
})(window.wesDDR = window.wesDDR || {}, jQuery);
-
Sep 22nd, 2013, 07:17 PM
#4
Re: VS when "debugging" Javascript does not like this technique
Is "fullremakeonsave" supposed to be a property or a method? Did you see errors in the other browser consoles (Firefox web inspector, Chromium devtools, Opera dragonfly)?
-
Sep 22nd, 2013, 07:30 PM
#5
Re: VS when "debugging" Javascript does not like this technique
I am trying to get the original error back again - but I've messed with so many settings related to IE debugging and IE script debugging and such.
Argh!!
At any rate - I'm not really having an issue here as much as I wanted to know that I not doing a bad thing with ||'ing like that.
-
Sep 23rd, 2013, 02:29 AM
#6
Re: VS when "debugging" Javascript does not like this technique
Originally Posted by szlamany
At any rate - I'm not really having an issue here as much as I wanted to know that I not doing a bad thing with ||'ing like that.
You do have to be careful that the left operand of the || is not accidentally "truthy" when you really wanted it to be "falsey".
http://james.padolsey.com/javascript/truthy-falsey/
-
Sep 23rd, 2013, 02:33 AM
#7
Re: VS when "debugging" Javascript does not like this technique
You could also golf it a bit by removing the second ||.
JavaScript Code:
var blnFullReMakeOnSave = !!(wesDDR.data("awcoptions") || {}).fullremakeonsave;
http://www.bennadel.com/blog/1784-Us...pe-Casting.htm
-
Sep 23rd, 2013, 04:31 AM
#8
Re: VS when "debugging" Javascript does not like this technique
-
Sep 23rd, 2013, 07:19 PM
#9
Re: VS when "debugging" Javascript does not like this technique
Originally Posted by szlamany
Nice links - thanks!
And one more for good luck...
http://net.tutsplus.com/tutorials/ja...in-javascript/
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
|