#+title: free function A free function is a [[file:20210307152738-programming_languages.org][programming language]] construct where a [[file:20210521211721-function_programming.org][function]] is not bound to a particular object, such as a [[file:20210521211757-method_programming.org][method]]. In the following example, =f= is a free function while =obj.m= is a method (=obj.m= can reference =this=[fn:1] ): #+begin_src js function f() { return "f"; } const obj = { m: function () { return "m"; }, }; #+end_src * Footnotes [fn:1] This example is somewhat misleading because in [[file:20200720132835-javascript.org][JavaScript]] a free function can become a method. Consider the following example: #+begin_src js function f() { return this.name; } const obj = { name: 'obj', }; console.log('free func:\t', f()); obj.f = f; console.log('method:\t', obj.f()); #+end_src #+RESULTS: : free func: undefined : method: obj : undefined Further, in [[file:20200720132835-javascript.org][JavaScript]], =this= can be set in free functions: #+begin_src js function f() { return `my name is ${this.name}`; } console.log(f.call({name: 'foo'})); #+end_src #+RESULTS: : my name is foo : undefined