Your example is wrong. You can't attach object properties to a function.
Javascript context is pretty simple. The keyword 'this' by default points at the object that the method belongs to. When you invoke this function :
myRide.drive();
the myride object owns the drive function, so any keyword 'this' inside of the drive function will be referring to the myride object.
when you do this:
letsDrive = myRide.drive;
letsdrive is now a function that belongs to the global object (window) since we did not declare an object for it. So 'this' is reffering to the window object.To get the output in your example, you would say
window.model = 'Mercedes';
And invoking letsdrive() will now return the expected result
I stand corrected on not being able to attach properties to a function, but what I said still holds true. The keyword this refers to the object that owns the method. The original commenter was saying that it was the method itself that was what this points at.
Javascript context is pretty simple. The keyword 'this' by default points at the object that the method belongs to. When you invoke this function :
myRide.drive();
the myride object owns the drive function, so any keyword 'this' inside of the drive function will be referring to the myride object.
when you do this:
letsDrive = myRide.drive;
letsdrive is now a function that belongs to the global object (window) since we did not declare an object for it. So 'this' is reffering to the window object.To get the output in your example, you would say
window.model = 'Mercedes';
And invoking letsdrive() will now return the expected result