method-chaining

Return self in python [closed]

﹥>﹥吖頭↗ 提交于 2019-12-02 18:59:13
I have a class that represents object. And I have a bunch of methods which modify this object state with no obvious return or obviously without any return. In C# I would declare all these methods as void and see no alternatives. But in Python I am about to make all the methods return self to give myself ability to write awesome one-liners like this: classname().method1().method2().method3() Is this Pythonic, or otherwise acceptable in Python? Here is a mail from Guido van Rossum (the author of the Python programming language) about this topic: https://mail.python.org/pipermail/python-dev/2003

Combining promises and chaining

拈花ヽ惹草 提交于 2019-12-02 10:50:25
Is there a good design pattern for utilizing promises, while still supporting chaining? For example, let's say that we have something like: function Foobar() { this.foo = function() { console.log('foo'); return this; }; this.bar = function () { console.log('bar'); return this; }; } var foobar = new Foobar(); foobar.foo().bar(); If instead we change the methods to use promises, e.g. function Foobar() { this.foo = function() { var self = this; return new Promise(function (resolve, reject) { console.log('foo'); resolve(self); }); }; ... } Is there a good way to do something like: var foobar = new

PHP method chaining benefits?

孤人 提交于 2019-12-01 21:02:02
Still on the PHP-OOP training wheels, this question may belong on failblog.org . =) What are the benefits of method chaining in PHP? I'm not sure if this is important, but I'll be calling my method statically. e.g. $foo = Bar::get('sysop')->set('admin')->render(); From what I've read, any method which returns $this is allowed to be chained. I just learned this is new in PHP5. Seems to me there may be speed benefits if I don't have to instantiate a whole new object (calling it statically) and just select the few methods I need from the class? Do I have that right? There are no significant

What are downside and advantage of method chaining in jQuery? [closed]

此生再无相见时 提交于 2019-12-01 18:17:31
What are downside and advantage of method chaining in jQuery ? Is it faster than re-declaring selector? Most probably the advantages are, that It makes your code short and easy to manage.It gives better performance(faster). And the chain starts from left to right. So left most will be called first and so on. When chaining is used JQuery has to find the elements once and it will execute all the attached functions one by one. An disadvantage of chaining can be using it unnecessarily too much then it can cause performance degrading. eg:- Code 1: ​$(document).ready(function(){ $('#myContent')

What are downside and advantage of method chaining in jQuery? [closed]

蹲街弑〆低调 提交于 2019-12-01 18:03:44
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . What are downside and advantage of method chaining in jQuery ? Is it faster than re-declaring selector? 回答1: Most probably the

jQuery - Chaining custom functions

陌路散爱 提交于 2019-12-01 18:02:10
I am wondering how to chain my custom functions and maintain context of 'this'. Example: $.fn.foo = function() { var html = '<div class="foo"></div>'; if ($(this).hasClass(somthing) { $(this).prepend(html); } } $.fn.bar = function() { var html = '<h3>bar</h3>'; $(this).find('.foo').prepend(html); } $('body').foo().bar(); When i try to use this code i get a TypeError: Cannot read property 'bar' of undefined You need to return current element context, i.e. this from you custom method. $.fn.foo = function() { var html = '<div class="foo"></div>'; if ($(this).hasClass('somthing')) { $(this)

jQuery - Chaining custom functions

别等时光非礼了梦想. 提交于 2019-12-01 18:00:40
问题 I am wondering how to chain my custom functions and maintain context of 'this'. Example: $.fn.foo = function() { var html = '<div class="foo"></div>'; if ($(this).hasClass(somthing) { $(this).prepend(html); } } $.fn.bar = function() { var html = '<h3>bar</h3>'; $(this).find('.foo').prepend(html); } $('body').foo().bar(); When i try to use this code i get a TypeError: Cannot read property 'bar' of undefined 回答1: You need to return current element context, i.e. this from you custom method. $.fn

Keep object chainable using async methods

[亡魂溺海] 提交于 2019-12-01 03:31:08
问题 Let's say I have a class Test with around 10-20 methods, all of which are chainable. In another method, I have some asynchronous work to do. let test = new Test(); console.log(test.something()); // Test console.log(test.asynch()); // undefined since the async code isn't done yet console.log(test.asynch().something()); // ERROR > My goal is to make this Since every other method is chainable, I feel like it would be weird for the user if this sole method isn't. Is there a way for me to maintain

Underscore.js _.tap() function what is a method chain?

荒凉一梦 提交于 2019-11-30 17:25:07
问题 The Underscore.js documentation explains that the _.tap() function "taps" into a method chain. http://underscorejs.org/#tap I have trouble following their example: _.chain([1,2,3,200]) .filter(function(num) { return num % 2 == 0; }) .tap(alert) .map(function(num) { return num * num }) .value(); => // [2, 200] (alerted) => [4, 40000] What is the method chain in this context? I always thought of method chaining as the concept of chaining methods off of one another: object.foo().bar().baz() . I

Swift - Method chaining

[亡魂溺海] 提交于 2019-11-30 13:50:48
I'd like to implement method chaining in my swift code, likely to Alamofire methods. For example, if I have to use my function like below getListForID(12).Success { // Success block }. Failure { // Failure block } How would I create the function getListForID ? To expand on the great points @dasblinkenlight and @Sulthan have made – here's a small example of how you could achieve your request function to take a success and failure closure, in the convenient syntax that you want. First, you'll have to define a new class to represent the 'result handler'. This is what your success and failure