Strange console.log behaviour with object and it's field [duplicate] - javascript

This question already has answers here:
console.log() async or sync?
(3 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 1 year ago.
I have the strange behavior (as i think) of console.log. So, there is two console.log in my console, first shows PdfViewerApplication object, second shows it's fieid, named eventBus.
So, as you can see second line says that PdfViewerApplication.eventBus right?
Now lets look at first line (before! second) , expand the body of the object and find eventBus field.
Obviously it is not null there, it almost has 2 eventListeners (pagesloaded)!
How is it possible?

Related

Strange browser js console behavior [duplicate]

This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
console.log() async or sync?
(3 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 7 months ago.
Hey mods the other question you linked had nothing to do with mine. Even on that questions page it says that the behavior doesn't exist anymore.
To preface, this isn't an issue with the variable i in the closures all being the same although it might look like it is.
The following code has a weird behavior. Basically the code runs the body of a for loop 3 times where it creates a set timeout that modifies and logs the same array.
const obj = {a:1,b:{c:2}};
console.log(obj);
obj.b.c = 3;
I expected when expanded the log would show that c was 2, but it actually shows that c is 3.
Why does this happen?
(I already answered below)
What is happening is that when you console.log a object only the first level of properties are stored before expanding in the console(clicking the > on an object property and seeing its properties). Think about the alternative, you deep copy every object that is logged. Then because of properties like __proto__ and constructor you would have to copy many many items. And if the object happens to have some reference to window you would practically have to take a memory snapshot.

The same array has different lengths? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
console.log(array) returns filled array, but console.log(array.length) is 0? [duplicate]
(1 answer)
Closed 3 years ago.
Can someone please explain to me, because I don't understand something here, why the same array has two lengths? I assume I understand something wrong here, it must have the same length, right?
I have this call:
console.log(eventsBeforeRender);
console.log(eventsBeforeRender.length);
and in browser console i see this:
What I want to achieve, is to access the element [0] of the root array, so then from this element I can access other elements, of which from what I see there should be 17.
Calling this:
console.log(eventsBeforeRender[0]);
returns undefined.

Javascript objects returns different values for a field when printed individually or whole [duplicate]

This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 3 years ago.
I have the following javascript line:
console.log({fieldMeta__value: fieldMeta.value, fieldMeta: fieldMeta});
As you can see, I am printing to console the object fieldMeta and, also, its field value. The problem is that its field value (fieldMeta.value) returns 1, but the whole objects prints the content for value as undefined:
I am pretty sure I have some flaw in my logic, but I can't find it.
EDIT
I tried another approach for printing the values:
console.log(fieldMeta);
console.log(fieldMeta.value);
And I got the same result:

What is the significance of `:` sign in JS [duplicate]

This question already has answers here:
What is the point of using labels in javascript (label: stuff here) outside switches?
(2 answers)
labeled statement - incorrect definition?
(1 answer)
Closed 3 years ago.
In javascript what does : mean except ternary condition.
Like if we write
$:1 returns 1
undefined:1 returns 1
a:1 returns 1
but
1:1 returns error
can someone please explain the scenario.

Why does a function stored in an Object return undefined in console at the end of running? [duplicate]

This question already has answers here:
Why does console.log say undefined, and then the correct value? [duplicate]
(5 answers)
Closed 7 years ago.
Was playing around with creating 'class objects'? if that is the correct term.
var cat = {
eyes:2,
pur: function() {
console.log("puuuuurrrrr");
}
};
cat.pur();
In Chrome this returns the console.log message and then on the next line undefined. Was wondering whats causing the undefined to pop up at the end. It doesn't do it when i call cat.eyes. In internet explorer this happens before the console.log() event.In nodeJS this happens after console.log.
you see first in the console what you wrote to the console which is puuuuurrrrr
the undefined is the output of pur() which is nothing meaning undefined.
if you would change pur to
console.log('whatever....');
return 'something';
than you will see instead of 'undefined' the value 'something'.
hope that explains it.

Categories

Resources