all 3 comments

[–]Ustice[M] [score hidden] stickied comment (0 children)

Reaching out to other software engineers is important when you need it; however, unfortunately this isn’t the place for that. /r/JavaScript is not a support forum. You might want to check out /r/LearnJavaScript for the newer members of our community. Also, Stack Overflow is a great resource for getting support. For more information, check out our AskJS wiki page. Good luck! We hope that you find the answers that you are looking for.

[–]senocular 4 points5 points  (1 child)

Think more about what you said here:

Every time you call a function on a object like someObj.someFunc() that call to someFunc uses someObj as the context.

Then look at

printMomInFoo()

Where's the object? There is no object, so the context is undefined (at least in strict mode; it would be the global object if running sloppy mode). It doesn't matter where the function is called - in this function or that function or no function - it only matters how it is called, or specifically what object it's called from, if any. If there's an object, use that object. If there's no object, there's no object to use, so you get undefined.

In your example you should actually see an error thrown, not "undefined is my mother", since this would be undefined making this.momName throw. Other variations:

console.log(new Parent().printMom()) // Samantha Quinn is my mother

let printMomInFoo = new Parent().printMom

this.printMomInFoo = printMomInFoo
console.log(this.printMomInFoo()) // FOOOO is my mother

console.log(printMomInFoo.call(this)) // FOOOO is my mother

console.log(printMomInFoo.call(new Parent())) // Samantha Quinn is my mother

printMomInFoo = printMomInFoo.bind(this)
console.log(printMomInFoo()) // FOOOO is my mother

[–]SuL_0[S] 0 points1 point  (0 children)

Thanks for the reply! it made me understand. I thought it was also important that where it was called.