1

I am new to Javascript, but have Java experiance.

const person = {
  name: "bin",
  walk() {
    console.log(this);
  }
};
person.walk();
const per1 = person;
per1.name = "cin";
per1.walk();

const animal = {
  type: "2",
  m2() {
    console.log("type");
  }
};
const walk1 = person.walk.bind(animal);
walk1();
person.walk();

I am failing to understand the output at line number 1 which prints a value of cin for name. When the code executes at person.walk(), the value of name has not changed. Why is the value being printed as cin?

{name: "bin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{type: "2", m2: ƒ}type: "2"m2: ƒ m2()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
Tech User
  • 137
  • 1
  • 3
  • 16
  • `person === per1`. Both the first and second logs point to the same reference. Browser's console shows the updated object every time you expand an object. Use `console.log(JSON.stringify(this))` – adiga Dec 22 '19 at 10:10

0 Answers0