Function rewriting different responses in Atom vs. Chrome console? - javascript

I placed this bit of code in both atom and the chrome console.
function a() {
alert('A');
return function() {
alert('B');
};
}
a = a();
In the chrome console it works likes its supposed to, the first time it is run it shows A the second time it is run it shows B. However, in the case of using Atom each run shows A and it never switches to b? Why is this?

Related

Why VS doesn't give me the same result as console in DevTools?

I'm sorry if this questions is being raised but I'm really new to coding and started to have the first difficult issue and can't figure it out. I have write a simple code like this because I have to learn the return command:
function multiplyByNineFifths (number) {
return number *(9/5);
};
function getFahrenheit (celcius) {
return multiplyByNineFifths (celcius) + 32;
};
getFahrenheit(15);
but when when I want to run the code in VS it says:
[Running] node "c:\Users\Oerd Bej\Desktop\JavaScript
exercises\tempCodeRunnerFile.js"
[Done] exited with code=0 in 0.088 seconds`
while in DevTools in console it runs perfectly and it gives me the right answer which is 59.
Please can you help me to figure it out what should I do, I have installed node.js and all the necessary plug in in VS but dont know what is wrong or what is the right question that I have to ask in order to understand it well. Every error in VS it breaks my heart
It's because logging is a little different in Chrome than in Node. If you type console.log(3) in Chrome, it will log 3, and then the return value of console.log which is undefined (because console.log doesn't return anything). Chrome will always automatically log the function's return value. Node doesn't. It will only log 3. Node executes the getFahrenheit function, but this function doesn't return anything. It just computes a number in memory, that's it, so nothing gets displayed. If you want to see the result of this operation you need to log it with console.log(getFahrenheit(15)).

How to run code on objects from current context when app is breakpointed

I put a breakpoint in Chrome DevTools in some step in my app. The code runs then gets to the breakpointed place. Can I somehow run pieces of code inside current context?
For example, I have a local variable profileDialog which is an object with methods and properties. Can I run them and add some custom debug code right in the context of the current breakpoint?
Once a breakpoint has been hit in the debugger, you can view the object's current state in the Scope panel on the right side.
You can also run code in the Console. For example, after you hit the breakpoint that sets the value of profileDialog, once you run to the next statement, you can put: profileDialog.show(); in the Console to execute that function.
If you want to inject some custom debug code for all calls to a function programmatically, you can run the following in the Console, where myObj is available in the current context:
var originalDoSomething = myObj.doSomething;
myObj.doSomething = function () {
console.log("About to call 'doSomething'");
return originalDoSomething.apply(this, arguments);
}

How to debug dynamic JavaScript functions in FireBug

I have some JavaScript functions like this:
function onSelectRow_${itemid}(){
something;
}
This it is appearing like this in Firebug script tab:
function onSelectRow_87878(){
something;
}
I put multiple break points (it has more than 20 lines, I put one in for example) in Firebug -> Script tab.
But the problem is, Firebug is not able to do debug these methods, ie. it is not stopping execution it executing as usual. I tried multiple times.
This is my actual code and use:
function onSelectRow_${escapedId }(rowId){
}
<jqgrid:grid onSelectRow="onSelectDeviceRow_${escapedId }"
What can I try to resolve it?
You can use debugger
function onSelectRow_87878()
{
debugger; //add here
something;
}
When you open Firebug, enable Script,and it will automatically go to the debugger point
debugging in firefox

Javascript on Firefox stops execution unexpectedly on an onchange assignment

The following javascript code snippets runs perfectly on Google Chrome but doesn't continue before the second alert on Firefox:
init.js:
function __init__() {
alert("welcome!");
document.getElementById("myselector").onchange = foo;
alert("must go here");
// some other initialization code goes here
alert("must end here");
}
window.onload = __init__;
foo.js:
function foo() {
// some other code for function foo
break;
// the rest of the code
}
There were no errors reported by the Firefox or Chrome's Developer Tool Console however, keeping me frustrated for days.
Remove any break statement when not using it inside loops or switch statements.

IE9 not running javascript onload

For some reason, IE9 is not running my JavaScript code onload when the browser is launched for the first time that session. It seems to only run onload after the user refreshes the page. It will also run the JavaScript when the debug console is open.
How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?
I'll restate this so you understand: The code DOESN'T run if you go to the site after launching a new browser session. The code DOES run if you open the site in a new tab, or reload the page, or open the debug console
Here is the function I use to run my script onload (which works fine in NORMAL browsers):
(function (i) {
var u = navigator.userAgent;
var e = /*#cc_on!#*/
false;
var st = setTimeout;
if (/webkit/i.test(u)) {
st(function () {
var dr = document.readyState;
if (dr == "loaded" || dr == "complete") {
i()
} else {
st(arguments.callee, 10);
}
}, 10);
} else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
document.addEventListener("DOMContentLoaded", i, false);
} else if (e) {
(function () {
var t = document.createElement('doc:rdy');
try {
t.doScroll('left');
i();
t = null;
} catch (e) {
st(arguments.callee, 0);
}
})();
} else {
window.onload = i;
}
})(init); //init is the function to call onload
I had the exact same issue that you had. I had a set of images that I wanted to ensure were preloaded before I began starting a slideshow. I was making use of
$(window).load(function(){
//All my code
});
And this is exactly what I was facing.
When I copied and pasted the URL in IE, the onload event did not seem to fire.
If I open the console using F12 and then past the URL in the browser and pressed enter, the everything seemed to be working.
Now that I opened the console at least once,
If I closeed the console and then reloaded the page, the onload was firing.
If I typed the URL and then pressed enter, the onload was firing.
It took me a couple of days to actually figure out what I was doing wrong.
The issue was with the console.log statements. At a lot of places in my code, I had done a lot of console logging. Even one of the plugins that I was using - jplayer has a an uncommented console message somewhere in the code.
The issue was that, unless you open the console at least once in IE, the console object is not available. Which means that the code will fail at the first console.log that it encounters.
Now, I was in no mood to comment out all my console.log statements just for the sake of testing it in IE. So, this is what I did instead. Right at the top of my document.ready jquery function, I wrote this small snippet of code.
if(!window.console){
console={};
console.log = function(){};
}
What it basically does is creates a dummy console.log function placeholder so that the code can run in IE but it will work only as long as console.log is the only console function that you are making use of in your code or in your plugins.
Just my 2 cents. Been pulling my hair over this issue for longer than I care to admit. I hope this is useful to at least someone.
You need to figure out if the code doesn't run at all, I.e. never enters your function, or if it fails on some specific line inside your function. Does IE9 show any warnings or js errors?
The easiest thing to do is stick a bunch of alert() statements in the code to see where it stops and narrow down to that line.
If it never enters your function then you need to look higher, where the call is being made.
Just a small note; When you use any debugging keywords (like console.log) or anything related, IE9 will escape this JS function if and only if the debugger is not on (with F12)
Actually I don't know what else cause a problem, but for me, my problem was the word "console.log" while debugger not on in IE9 ... I know this is already an answered question, but I felt it needs to be be known.
Okay, I figured it out. It has to do with some weird way IE handles IF statements.
In my init function I had two IF statements, one which checked if a variable existed and then logged the value of that variable. The other which checked to see if the value of the same variable was equal to an arbitrary string.
After removing the first IF statement, everything seems to work properly. I also decided to use a different onload function which can be seen below:
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', init, true);
} else if (document.all && !window.opera){ //Crude test for IE
//Define a "blank" external JavaScript tag
document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
var contentloadtag=document.getElementById("contentloadtag");
contentloadtag.onreadystatechange=function(){
if (this.readyState=="complete") {
init();
//ie('open');
}
}
}

Categories