Skip to content Skip to sidebar Skip to footer

Console.log A Javascript Object/class - Same Result Before And After The Change Of The Prototype

I'm trying to understand how js prototypes and classes work, and I'm using Chrome's console.log to print and have a look at the state of my objects while I add new properties etc.

Solution 1:

your next line of code where you set the persons surname, doesnt wait for the console log because console.log is asynchrounous, when you try out this code with a timeout it will be correct,

functionPerson() {}

 Person.prototype.sayHello = function () {
     alert("Hello");
 };
 Person.prototype.name = "Name";

 console.log(Person.prototype) //1st console.logsetTimeout(function(){
  Person.prototype.surname = "Surname";

 console.log(Person.prototype); //2nd console.log
 },1000);

you could save a copy of that object before you log it, then it would work

Synchronous console logging in Chrome

UPDATE: i have an even better solution : just log a stringifyed version of the object and you´ll be okay

console.log(JSON.stringify(Person.prototype))

Post a Comment for "Console.log A Javascript Object/class - Same Result Before And After The Change Of The Prototype"