Started learning Javascript and I wrote something like this in FireBug of FireFox:
var myObject = {
"first_name" : "Rick",
"last_name" : "Hummer"
};
var name;
for (name in myObject) {
if(typeof myObject[name] != 'function') {
(name + ' : ' + myObject[name])
}
}
When I run it, it only shows the last name, shouldn't it also list first name ?
Plus how can I put break points and debug this anyway?
enter image description here
-
1Ypu can add breakpoints and see each line of execution getfirebug.com/javascripthop– hop2013年04月04日 15:58:40 +00:00Commented Apr 4, 2013 at 15:58
3 Answers 3
You didn't tell the browser to output the values.
By default it writes out the result of the last executed line which is (name + ' : ' + myObject[name])
To solve this simply add console.log:
var myObject = {
"first_name" : "Rick",
"last_name" : "Hummer"
};
var name;
for (name in myObject) {
if(typeof myObject[name] != 'function') {
console.log(name + ' : ' + myObject[name]);
}
}
Works for Firefox 19: Demo
3 Comments
You have a statement that doesn't do anything, it builds a string but doesn't use it. The debugger is probably just showing you the last statement that was executed.
Change
(name + ' : ' + myObject[name])
To:
console.log(name + ' : ' + myObject[name])
and you should see both keys appear in the log.
Comments
Also, for debugging, you should simply write 'debugger' (without any quotes) at the line you want to put a breakpoint. As I know all browsers support that statement.