0

I am trying to understand why this is global when I have specifically assigned the function to an object with the value.

I got this code from a website.

var obj1 = {
    name: "Pulsar",
    bike: function() {
       console.log(this.name);
    }
}
var obj2 = { name:"Gixxer", bike: obj1.bike };
var name = "Ninja";
var bike = obj1.bike;

bike();           // "Ninja"
obj1.bike();      // "Pulsar"
obj2.bike();      // "Gixxer"

My question is, the bike() function is part of obj1.bike, so why does bike() result in Ninja, and not pulsar like obj1.bike, as it is similar?

SherylHohman
  • 12,507
  • 16
  • 70
  • 78
NewtoJS
  • 1
  • 1

0 Answers0