javascript - console.log() async or sync? -


i reading async javascript trevor burnham. has been great book far.

he talks snippet , console.log being 'async' in safari , chrome console. unfortunately can't replicate this. here code:

var obj = {};  console.log(obj);  obj.foo = 'bar'; // outcome: object{}; 'bar'; // book outcome: {foo:bar}; 

if async, anticipate outcome books outcome. console.log() put in event queue until code executed, ran , have bar property.

it appears though running synchronously.

am running code wrong? console.log async?

console.log not standardized, behavior rather undefined, , can changed release release of developer tools. book outdated, might answer soon.

to our code, not make difference whether console.log async or not, not provide kind of callback or so; , values pass referenced , computed @ time call function.

we don't know happens (ok, could, since firebug, chrome devtools , opera dragonfly open source). console need store logged values somewhere, , display them on screen. rendering happen asynchronously sure (being throttled rate-limit updates), future interactions logged objects in console (like expanding object properties).

so console might either clone (serialize) mutable objects did log, or store references them. first 1 doesn't work deep objects. also, @ least initial rendering in console show "current" state of object, i.e. 1 when got logged - in example see object {}.

however, when expand object inspect properties further, console have stored reference object , properties, , displaying them show current (already mutated) state. if click on +, should able see bar property in example.

here's screenshot posted in bug report explain "fix":

so, values might referenced long after have been logged, , evaluation of these rather lazy ("when needed"). famous example of discrepancy handled in question is chrome's javascript console lazy evaluating arrays? workaround make sure log serialized snapshots of objects always, e.g. doing console.log(json.stringify(obj)). work non-circular , rather small objects only, though. see how can change default behavior of console.log? (*error console in safari, no add-on*).


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -