Chrome's JavaScript Console Behavior: Evaluating Objects Lazily
The behavior of the Chrome JavaScript console when evaluating objects has been observed as potentially unexpected. To illustrate this, consider the following code:
In Firefox, the console outputs:
However, in Chrome version 7.0.517.41 beta, the output is:
Unexpected Evaluation Behavior
This behavior suggests that Chrome's console may be "lazy" when evaluating objects. When the first console.log statement is executed, the console references the object in memory. Any subsequent modifications to the object, such as changing the first element from "hi" to "bye," are not reflected in the output of the first console.log statement. This implies that the console only evaluates the object at the time of the first console.log statement and stores the snapshot.
Bug Explanation
This behavior is documented in the following Webkit bug report: https://bugs.webkit.org/show_bug.cgi?id=35801 (now fixed). The bug states that Chrome's console does not perform lazy evaluation for objects that are accessed directly, resulting in the observed behavior.
Workaround
To prevent this unexpected behavior, one can call toString on the object before passing it to console.log. This forces Chrome to create a representation of the object that is not altered by subsequent changes, ensuring that the console output reflects the current state of the object.
Output:
The above is the detailed content of Why Does Chrome's JavaScript Console Show Unexpected Results When Evaluating Objects?. For more information, please follow other related articles on the PHP Chinese website!