Skip to content

Method chaining lets the receiver nominate a next receiver #1673

@erights

Description

@erights

Re https://github.com/airbnb/javascript#constructors--chaining

Code like

luke.jump().setHeight(20);

enables luke to provide something other than luke itself as the receiver of the setHeight message. When this delegation of control is intended, such method chaining is great. OTOH, if the caller assumes it is equivalent to

luke.jump();
luke.setHeight(20);

then they depend on a contract that luke might not obey. That doesn't necessarily mean the style should not be used, but the hazard needs to be explained clearly whenever this style is encouraged.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions