Objects, Prototype constructor function stange behaviour in jsfiddle VS

// constructor function function Person () { this.name = 'John', this.age = 23 } // creating objects const person1 = new Person(); const person2 = new Person(); person2.name = 'Joe' // adding property to constructor function Person.prototype.gender = 'male'; // prototype value of Person console.log(Person.prototype); // inheriting the property from prototype console.log(person1); console.log(person2); console.log(person1.gender); console.log(person2.gender); console.log(person1.name); console.log(person2.name);

I don’t see why console.log(person2); does not log the persons gender from the prototype.
I can only get it to log the gender when specifying ‘person2.gender’.
I don’t see how this can make sense, I expect that console.log(person2) should output name, age and gender. strangely I get this behavior on my local Node.js instance (Node version 19.7) and codebyte, but when I run it on JS fiddle the output is what I originally expected (see Edit fiddle - JSFiddle - Code Playground)

Any thoughts?

It’s possible that the behavior you are seeing in your Node.js instance and Codebyte is due to a version compatibility issue or a difference in how these environments handle prototype inheritance.

In general, when you log an object to the console, it will show all the properties and methods of that object, including those inherited from its prototype. However, in some cases, the console may not display inherited properties, or may display them differently than you expect.

In your example, since you have added the “gender” property to the prototype of the Person constructor function, all instances of Person should inherit that property. Therefore, both person1 and person2 should have a “gender” property, and you should be able to access it by calling person1.gender or person2.gender.

If you’re not seeing the “gender” property when logging person2 to the console, it’s possible that there is some issue with the console itself or the environment you’re running the code in. It’s also possible that there is a mistake in your code that is causing the behavior you’re seeing.
(I don’t see one though)

I would recommend trying your code in different environments and consoles to see if the behavior is consistent, and double-checking your code to make sure there are no errors or typos.