Banjo

52 thoughts
last posted Nov. 9, 2015, 7:13 p.m.

14 earlier thoughts

0

Bound vs unbound methods

In Python when you reference a field in an object, and that object is a function in the object's class, it creates a "bound" method for you to call that injects that magical "self" parameter into the argument list. Conceptually this means that the "call" operation is unaware of objects and methods, it just operates on callable.

In javascript, on the other hand, methods are not bound by default. Instead, when a call is made, it does things differently for a method call - it sets the hidden "this" parameter based on the object the function was fetched from. So the "call" operation has to know whether a method is being called and, if so, what object it is called on.

Current thought:

  • When evaluating an object slot / property, self is bound to the object the property pulled from
  • If the slot returns a function/method closure this "self" is available in lexical scope and is bound as part of the closure if necessary, essentially creating a bound method
  • Functions also can bind a name for themselves as a slightly different kind of "self", referring to the function currently being called rather than the object a method is being invoked on

37 later thoughts