0

Possible Duplicate:
Why does javascript object show different values in console in Chrome, Firefox, Safari?

I'm quite new to be using javascript's OOP concepts. I'm trying to understand the binding of javascript properties. Does javascript execute the below code, sequentially?

 // main.js
 function NameClass(){
   this.name = "John"
   this.age = 25
 }

 NameClass.prototype.change_my_name = function(new_name){
   this.name = new_name
 }

 NameClass.prototype.change_my_age = function(new_age){
   this.age = new_age
 }


 // main.html
  <html>
    <head>
      <title>Javascript tutorial</title>
        <script src="main.js"></script>
        <script>
          var nc = new NameClass()

          console.log("nc before modification")
          console.log(nc) // Prints Doe

          nc.change_my_name("Doe")

          console.log("nc after modification")
          console.log(nc) // Prints Doe
        </script>
      </head>
    <body></body>
  </html>

Now, what is trick here?

  1. Why do I see "Doe" display both the times?
  2. What is the mechanism that I've to use here in order to display "John" the first time and "Doe" in the second?
Community
  • 1
  • 1
Vineeth Pradhan
  • 8,201
  • 7
  • 33
  • 34

1 Answers1

4

console.log() will not necessarily log the value the variable had at the time you tried to log it.

Frederik H
  • 1,406
  • 2
  • 13
  • 15
  • ... without an interrupt. Do the same thing with an 'alert'. You'll see the value change. – Jarrett Meyer Oct 26 '12 at 17:51
  • ... or test it with QUnit. You'll see the two different values. – Jarrett Meyer Oct 26 '12 at 17:51
  • Or just get the desired log value as a string (not an object) and log that. It works when using strings, just not when using objects. – jfriend00 Oct 26 '12 at 17:52
  • @jfriend00 : Firefox logs the object state as expected. The problem is in Chrome when we try to log objects. Though, logging strings works fine in both the browsers – Vineeth Pradhan Oct 26 '12 at 17:56
  • @Vineeth - yes I know it's a Chrome issue. I was suggesting that if you only pass a string to `console.log()`, then you don't have an issue in Chrome. – jfriend00 Oct 26 '12 at 17:57
  • @jfriend00 To be more specific, it works for generic types (strings and numbers) since they are passed by value. Objects are always passed by reference in javascript. The likely cause is that, since the console is a UI element, chrome's implementation waits until the next "redraw" (which would be at the end of the code block) before displaying the object. At that point, the object has changed, so each instance of `console.log` against that object will be the same. – Shmiddty Oct 26 '12 at 18:00
  • @Shmiddty .. Even the Objects are passed by value and not by reference .. But internally they point to the same memory location :) – Sushanth -- Oct 26 '12 at 18:16
  • All arguments in ECMAScript are passed by value. It is not possible to pass arguments by reference. – Sushanth -- Oct 26 '12 at 18:18
  • @Sushanth-- I supposed it's more of a pseudo-reference, as if you change any properties on the argument, it will change those properties on the original object (including adding and deleting properties), but setting the argument itself to a value/new object won't affect the original object. – Shmiddty Oct 26 '12 at 18:48