method-chaining

How to chain functions without using prototype?

巧了我就是萌 提交于 2019-11-28 07:35:15
I have a bunch of useful functions that I have collected during my whole life. function one(num){ return num+1; } function two(num){ return num+2; } I can call them with two(two(one(5))) But I would prefer to use (5).one().two().two() How can I achieve this without using prototype? I tried to see how underscore chain works, but their code is too intense to understand it The dot syntax is reserved for objects. So you can do something like function MyNumber(n) { var internal = Number(n); this.one = function() { internal += 1; // here comes the magic that allows chaining: return this; } // this

JavaScript Object Method Chaining: useful? [closed]

人盡茶涼 提交于 2019-11-28 06:54:26
So... messing around in JavaScript with an idea that's new to me, having methods of an Object return the Object of which they are methods; this then leads to chainability. My question, then: how can this be useful? I threw this together to test the fundamental workings: <script> MathChain = function() { this.pass = function() { this.multiply = eval(arguments.join('*')); this.add = eval(arguments.join('+')); return this; } } m = new MathChain().pass(5, 10, 20).multiply; // 1000 a = new MathChain().pass(5, 10, 20).add; // 35 </script> That's obviously not a viciously efficient instance in which

Method Chaining in Java [closed]

白昼怎懂夜的黑 提交于 2019-11-28 04:20:31
While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes. If I were to create a Car class for example, I could make it chainable by reutrning this instead of void as follows: public class Car { private String make; public Car setMake(String make) { this.make = make; return this; } } Is there any particular reason why the built in libraries don't tend to do things this way? Is there a downside to method chaining? I may have overlooked something which would explain the lack

method chaining in python

落花浮王杯 提交于 2019-11-28 04:10:55
(not to be confused with itertools.chain) I was reading the following: http://en.wikipedia.org/wiki/Method_chaining My question is: what is the best way to implement method chaining in python? Here is my attempt: class chain(): def __init__(self, my_object): self.o = my_object def __getattr__(self, attr): x = getattr(self.o, attr) if hasattr(x, '__call__'): method = x return lambda *args: self if method(*args) is None else method(*args) else: prop = x return prop list_ = chain([1, 2, 3, 0]) print list_.extend([9, 5]).sort().reverse() """ C:\Python27\python.exe C:/Users/Robert/PycharmProjects

Conditional Builder Method Chaining Fluent Interface

戏子无情 提交于 2019-11-28 02:46:23
I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level = 5; var ninja = NinjaBuilder .CreateNinja() .Named("Ninja Boy") .AtLevel(level) .WithShurikens(10) .WithSkill(Skill.HideInShadows) .When(level > 3) .Build() Update - A sample solution can be found here . What I'd do is have NinjaBuilder keep the operations as a list of delegates, rather than applying them, and only apply them when .Build is called.

Chaining methods with javascript

北城以北 提交于 2019-11-27 19:37:18
问题 I'm trying to create chaining with the javascript methods similar to what we have with jquery. Please let me know how to implement chaining with javascript. var controller = { currentUser: '', fnFormatUserName: function(user) { this.currentUser = user; return this.currentUser.toUpperCase(); }, fnCreateUserId: function() { return this.currentUser + Math.random(); } } var output = controller.fnFormatUserName('Manju').fnCreateUserId(); 回答1: As I already explained, since you are returning a

Basic method chaining

℡╲_俬逩灬. 提交于 2019-11-27 19:16:38
问题 I found this method chaining in python, but even with it I couldn't understand method chaining in Python. Here the goals are two: solve the coding problem and understand method chaining (given that I am still not 100% confident with callables). Down to the problem definition. I want a class that has two methods: one sets a parameter of the object = 'line' and the other overwrites to 'bar'. This is what I got so far: class foo(): def __init__(self, kind=None): self.kind = kind def __call__

How do I chain methods in PHP? [duplicate]

瘦欲@ 提交于 2019-11-27 14:19:24
This question already has an answer here: PHP method chaining? 8 answers jQuery lets me chain methods. I also remember seeing the same in PHP so I wrote this: class cat { function meow() { echo "meow!"; } function purr() { echo "purr!"; } } $kitty = new cat; $kitty->meow()->purr(); I cannot get the chain to work. It generates a fatal error right after the meow. Zachary Murray To answer your cat example, your cat's methods need to return $this , which is the current object instance. Then you can chain your methods: class cat { function meow() { echo "meow!"; return $this; } function purr() {

Method chaining + inheritance don't play well together?

谁都会走 提交于 2019-11-27 13:04:38
问题 Consider: // member data omitted for brevity // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc) struct Widget { Widget& move(Point newPos) { pos = newPos; return *this; } }; struct Label : Widget { Label& setText(string const& newText) { text = newText; return *this; } Label& setAngle(double newAngle) { angle = newAngle; return *this; } }; struct Button : Label { Button&

How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

两盒软妹~` 提交于 2019-11-27 09:33:39
Using method chaining, I am looking to fire a function repeatedly but only after the function has completed. Almost like don't execute until the function has fully run its course. Example of intended result: var myfunc = { copy: function(message){ window.setTimeout(function(){ console.log(message); },1000); return this; } }; myfunc.copy('hello').copy('world'); // wait a second then log: // hello // wait until first function completion (1000ms), wait a second then log: // world Any help is appreciated! MiltoxBeyond If you don't want to use jQuery you can also have it as a pure JS solution. var