JavaScript's this, Decorators, and the Call Stack for Pythonistas (Summary)

Congratulations, you made it to the end of the course! The translation desk’s quote pipeline now runs in JavaScript. It prints the same seven lines as the Python script, from Words: 400 and GBP 48.00 through GBP 60.00 to Calculations: 2, Setups: 1, and Audit entries: 3.

Getting there meant writing out jobs that Python does for you. A bound method carries its instance, and an @ line applies a decorator. lru_cache builds keys from argument values. In JavaScript, you choose the receiver, apply each wrapper by hand, and build each cache key yourself.

In this course, you learned how to:

  • Keep the object a method works for (its receiver) when you pass it along or save it for later
  • Decorate a call with an explicit wrapper that keeps its arguments, receiver and result
  • Cache only repeatable work, with a key that covers every input
  • Trace nested calls on the call stack, apart from the scope chain
  • Port the translation desk’s quote pipeline to JavaScript

Read the Receiver at the Call Site

In Python, saved = desk.format keeps the desk, because looking the method up binds self. In JavaScript, a regular function takes its this from the object before the dot in each call. Copied onto a euro desk, the same formatter gave EUR 48.00. Saved on its own and called as a plain function, it had no receiver. A module always runs in strict mode, where a plain call leaves this as undefined, so reading currency threw a TypeError.

When you need to choose the receiver yourself, .call(desk, 4800) and .apply(desk, [4800]) run the formatter now and return GBP 48.00. Neither one keeps the desk afterward.

Choose a Method, an Arrow, or bind()

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.