Reputation: 189
I have the following code
function Person(name){
console.log(this);
this.firstname=name;
}
var sam=new Person("Sam");
console.log(sam);
Output is - Response
When a new Object is created, initially this should point to an empty object. Why it has the updated response?
Upvotes: 2
Views: 49
Reputation: 71891
When you press the 'dropdown' icon in chrome console to inspect the object, it will -then- start evaluating the object itself at that memory location. So at the moment you press it, the property firstname
is already filled.
If you try:
console.log(this.firstname);
You will see:
undefined
Sam
You can also try it using:
console.log(JSON.stringify(this))
This will disable the ability to navigate through the object though. If it's just a plain object you can do this:
console.log(JSON.parse(JSON.stringify(this)))
Upvotes: 2
Reputation: 1
Firefox logs
Object { }
Object { firstname: "Sam" }
So, your browsers console is lying to you
Upvotes: 1