0

hello everyone want to understand the order of javascript code my code

class User {
  constructor(id, username) {
    this.i = id;
    this.u = username;
  }
  sayHello() {
    return `Hello ${this.u}`;
  }
}

let userOne = new User(100, "Elzero");
console.log(userOne);
User.prototype.sayWelcome = function () {
  return `Welcome ${this.u}`;
};

 Object.prototype.test = "Test";

result in console line 12

    User {i: 100, u: 'Elzero'}
i: 100
u: "Elzero"
[[Prototype]]: Object
sayWelcome: ƒ ()    **<-------------**
constructor: class User
sayHello: ƒ sayHello()
[[Prototype]]: Object
test: "Test"       **<-------------**
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
__proto__: (...)
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()

can some one tell why in line 12 when i display the userone instance of User i find sayWelcome() method and test property in the prottotype of the userone despite both of them are added after the line 12

Cordially

  • 1
    Don't trust the console to output a true snapshot of the object. It usually evaluates the object when interacted with. To see a "true" snapshot, try logging a JSON string of it? `console.log(JSON.stringify(userOne))` – evolutionxbox Jul 18 '22 at 16:46
  • @IrakliTchedia No, that has nothing to do with it – Bergi Jul 18 '22 at 17:25
  • Put a breakpoint right after the log (`console.log(userOne); debugger;`) and inspect the object while lines 13+ have not yet been executed. – Bergi Jul 18 '22 at 17:27

0 Answers0