Comments on: “Prototypal chainability” https://j11y.io/javascript/prototypal-chainability/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Matthew https://j11y.io/javascript/prototypal-chainability/#comment-824 Tue, 14 Apr 2009 20:15:28 +0000 https://j11y.io/?p=741#comment-824 (I suggest ‘method call chaining’. Or yeah, builder pattern.

]]>
By: Matthew https://j11y.io/javascript/prototypal-chainability/#comment-823 Tue, 14 Apr 2009 20:14:50 +0000 https://j11y.io/?p=741#comment-823 To be clear: this doesn’t really have much to do with prototype-based OO or chaining of prototypes in javascript.

It’s just a convention that can be used in any OO language where you ‘return this’ from methods.

Probably best not to name the technique in a way that leads to confusion with javascript’s prototype-chain-based object attribute lookup semantics.

]]>
By: SaturnPolly https://j11y.io/javascript/prototypal-chainability/#comment-822 Tue, 14 Apr 2009 12:43:06 +0000 https://j11y.io/?p=741#comment-822 In the context of object creation this is also called builder pattern. Here is an example from Effective Java (2nd Edition):

NutritionFacts sodaDrink = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();

see http://www.javaspecialists.eu/archive/Issue163.html for more details, they talk about some of the chapters in this book.

I find it irritating to use the terms ‘prototypal’ or ‘inheritence’ though, since neither is really involved in this approach.

]]>
By: Graham B https://j11y.io/javascript/prototypal-chainability/#comment-821 Mon, 13 Apr 2009 13:38:39 +0000 https://j11y.io/?p=741#comment-821 Chaining is a great technique if you like your scripts concise.

Just a small addition; one thing chains can’t do is give you returned data, as the returned object must always be the original instance. One way around this is to allow an optional callback parameter, so your method can return some data about the instance while invoking a new ‘chain’. Admittedly its not pretty and I don’t do it myself, but it works if you really must do everything on one line.

]]>