📚 node [[20210521211631 free_function]]

A free function is a programming languageconstruct where a functionis not bound to a particular object, such as a method

In the following example, f is a free function while obj.m is a method (obj.m can reference this1):

function f() {
    return "f";
}

const obj = {
    m: function () {
        return "m";
    },
};

Footnotes

1This example is somewhat misleading because in JavaScripta free function can become a method. Consider the following example:

function f() {
    return this.name;
}

const obj = {
    name: 'obj',
};

console.log('free func:\t', f());
obj.f = f;

console.log('method:\t', obj.f());

free func: undefined method: obj undefined

Further, in JavaScript this can be set in free functions:

function f() {
    return `my name is ${this.name}`;
}

console.log(f.call({name: 'foo'}));

: my name is foo : undefined

📖 stoas
⥱ context