I have code that looks something like this:
function pathfind (start,end,map)
{
this.Init = function ()
{
this.open_node = new Array();
this.open_node.push(start);
console.log(this.open_node);
this.Loop();
}
this.Loop = function ()
{
//Some code here
}
this.Init();
}
For some reason when I push "start" to this.open_node and I log its value, I get "undefined". However, after some bug testing I realized that commenting out this.Loop(); in this.Init causes push to function properly and console.log to return [start] as it should. Can anyone explain why on earth this behavior would occur?
EDIT: I'm calling
pathfind({x:2,y:2},{x:24,y:24},parsemap(25,25));
After further research I found that console.log doesn't execute immediately in Chrome. Hence the outdated reports.
Your code executes pathfind function that returns undefined(and it should be this way) but you wait for result from this.Init function. Should probably execute it instead of pathfind.
Related
Is there a way to set breakpoints when specific functions are about to execute?
It needn't be an explicit breakpoint - I just want execution to pause when a console.log() is about to be called.
Or should I resort to this method.
I prefer to accomplish this without modifying my code, or manually setting breakpoints at every console.log.
Yes that's the trick. Create a custom logging function, put debugger in it and call console.log and you have what you wanted:
function log(message) {
debugger;
console.log(message) ;
}
Edit:
You can also replace console.log by a similar fonction that calls the original:
var clog = console.log;
console.log = function(message) {
if(message == '...') {
debugger;
}
clog.apply(console, arguments);
}
This code will affect all log calls within your page. The check is to catch a certain message. Remove it to stop always.
How about call this at the very start that add a pause break for your every console.log?
This replace the original console.log, pause break first, then call the orignal console.log for you. And this will be apply on all console.log calls.
(function () {
var oldLog = console.log;
console.log = function() {
debugger;
oldLog.apply(console, arguments);
}
})();
console.log('hello');
I have a function main that has several inner functions like this:
function main_f (params) {
function do_this () {
// do this...
}
function do_that () {
do_this(); // working
main_f.parse_stuff(); // not working
parse_stuff(); // not working
}
do_that();
main_f.parse_stuff = function(){
console.log("success");
}
}
function second_f () {
main_f.parse_stuff(); //working
}
I was expecting that main_f.parse_stuff() would work inside do_that, but that is not the case. My questions are:
-Is it posible to call that method from inside main_f ? how?
EDIT: Execute do_that after parse_stuff is written.
-Why can't I call parse_stuff from main_f?
EDIT: I just realised that the function doesn't read on compilation time, but execution time, therefore it is not visible when do_that is called.
-How can I know every function on scope?
It is not possible by programation but you can do it with the debugger. Just insert a break point on that scope and you can check everything that is global, local and in the closure.
I checked this with chrome dev-tools.
In my jQuery scripts, when the user closes a menu with an animation, I have to call a function after the closing animation is finished. I want to assign this function dynamically by calling a function openStrip() with a parameter. My code looks like:
var FUNCTION_JUST_AFTER_MENU_CLOSE = function(){};
function openStrip(stripId){
FUNCTION_JUST_AFTER_MENU_CLOSE = function(){
createStrip(stripId);
});
}
if I call openStrip("aStripId"), I expect FUNCTION_JUST_AFTER_MENU_CLOSE to be:
// #1
function(){
createStrip("aStripId");
}
whereas my current code gives:
//#2
function(){
createStrip(stripId);
}
i.e, the parameter passed to the function openStrip() is lost while assigning the function() to the variable FUNCTION_JUST_AFTER_MENU_CLOSE.
How can I avoid this.
EDIT: I discovered that my code is actually working. The problem was elsewhere. I got confused because when I looked at Chrome's debugger, it was showing me the function definition as is (#2 in above). But when it actually went down executing that function later in the code, it did evaluate the values of the passed argument, and endedup executing #1.
Thanks for the answer though. I am marking it correct because that is perhaps a better way of assigning the function.
The best way is to return a function, from openStrip like this
function openStrip(stripId) {
return function() {
createStrip(stripId);
};
}
For example,
function openStrip(stripId) {
return function() {
console.log(stripId);
};
}
openStrip("aStripId")();
# aStripId
openStrip("bStripId")();
# bStripId
You can even assign the function objects returned to different variables and use them later on
var aStrip = openStrip("aStripId");
aStrip();
# aStripId
aStrip();
# aStripId
Despite excessive googling I just don't get why my function doSomething does nothing in the situation below. Any idea why it doesn't work?
Many thanks, Gordon
var arrAttend=new object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
It's a typo, you should use new Object (capital O). Or use an Object Literal:
var arrAttend = {Blob: 'hello'};
function doSomething() {
alert (arrAttend.Blob);
}
Two problems :
object isn't defined
you don't call your function
Try this :
var arrAttend= {}; // that's the simplest way to create a new javascript object
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
doSomething();
Note that the first kind of error is very easily found when you look at the console : an error is displayed. I'd suggest you to use developer tools (for example Chrome's ones) so that you don't develop in the blind. BTW you'd see that using console.log instead of alert is most often more convenient.
Try this :
var arrAttend=new Object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
There's typo error in your code. And an object should be used like follow -
var arrAttend= {
name:'Blob'
};
function doSomething() {
alert (arrAttend.name);
}
doSomething();
Try this:
// create object
var arrAttend=new Object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
// call function
doSomething();
I'm trying to create a javascript object that can call other methods within itself. However, I'm running into a weird problem that I just can't seem to figure out.
I have the following code
myObjectDef = function() {
this.init = function() {
//do some stuff
this.doSecondInit();
}
this.doSecondInit = function() {
//do some more stuff
}
}
myObject = new myObjectDef();
myObject.init();
I am getting an error that states "Message: Object doesn't support this property or method". And it ends at this.doSecondInit();. I can't quite figure out why it's doing this. My code runs great up to the call to the second method. How do I make this work?
There's an extra set of parenthesis here:
this.doSecondInit() = function() {
You can't assign to the result of a function call, let alone to the result of a function that doesn't even exist.
After your edit, your thing seems to work fine:
http://jsfiddle.net/nabVN/
You sure you didn't have the same typo in your actual code? Better start getting used to not putting that () after every function call, which is probably a bad habit carried over from languages where functions aren't values.