applyIt

applyIt(function(){ // do something here with this })

  • The context object is available as a receiver (this).

  • The return value is the object itself.

As applyIt returns the context object itself, we recommend that you use it for code blocks that don't return a value and that mainly operate on the members of the receiver object. The most common use case for applyIt is for object configuration. Such calls can be read as "apply the following assignments to the object."

const manuel = {name: "Manuel", age: 36};

manuel.applyIt(function () {
    this.name = "Manuel Santos";
    this.age++;
    (this as any)["country"] = "Portugal";
});
console.log(manuel)

Another use case for applyIt is to include applyIt in multiple call chains for more complex processing.

Last updated