Distinctions

Because scope functions are similar in nature, it's important to understand the differences between them. There are two main differences between each scope function:

  • The way they refer to the context object.

  • Their return value.

Context object: this or it

Inside the lambda passed to a scope function, the context object is available by a short reference instead of its actual name. Each scope function uses one of two ways to reference the context object: as a function receiver (this) or as a lambda argument (it). Both provide the same capabilities, so we describe the pros and cons of each for different use cases and provide recommendations for their use.

function main() {
    const str = "Hello"
    // this
    str.runIt(function () {
        console.log(`The string's length: ${this.length}`)
    } );

    // it
    str.letIt(it => {
        console.log(`The string's length: ${it.length}`)
    })
}

Last updated