chainable

PHP OOP: Chainable objects?

▼魔方 西西 提交于 2019-12-09 10:23:13
问题 I have tried to find a good introduction on chainable OOP objects in PHP, but without any good result yet. How can something like this be done? $this->className->add('1','value'); $this->className->type('string'); $this->classname->doStuff(); Or even: $this->className->add('1','value')->type('string')->doStuff(); Thanks a lot! 回答1: The key is to return the object itself within each method: class Foo { function add($arg1, $arg2) { // … return $this; } function type($arg1) { // … return $this;

Chainable, Promise-based Class Interfaces in JavaScript

谁说胖子不能爱 提交于 2019-12-07 11:58:43
问题 I am writing a constructor in JavaScript that has the following properties: function WhizBang() { var promise; this.publicMethod_One = function publicMethod_One() { ... }; this.publicMethod_Two = function publicMethod_Two() { ... }; promise = asyncInit(); } So, calling new WhizBang() will start the asyncInit() process. What isn't obvious from the code above is that none of the public methods in the interface should run until this call to asyncInit() has closed. So, the definition of

Chainable, Promise-based Class Interfaces in JavaScript

不问归期 提交于 2019-12-05 18:45:30
I am writing a constructor in JavaScript that has the following properties: function WhizBang() { var promise; this.publicMethod_One = function publicMethod_One() { ... }; this.publicMethod_Two = function publicMethod_Two() { ... }; promise = asyncInit(); } So, calling new WhizBang() will start the asyncInit() process. What isn't obvious from the code above is that none of the public methods in the interface should run until this call to asyncInit() has closed. So, the definition of publicMethod_One() might look something like this: function publicMethod_One() { promise .then( doStuff ) .catch

PHP OOP: Chainable objects?

非 Y 不嫁゛ 提交于 2019-12-03 12:54:04
I have tried to find a good introduction on chainable OOP objects in PHP, but without any good result yet. How can something like this be done? $this->className->add('1','value'); $this->className->type('string'); $this->classname->doStuff(); Or even: $this->className->add('1','value')->type('string')->doStuff(); Thanks a lot! The key is to return the object itself within each method: class Foo { function add($arg1, $arg2) { // … return $this; } function type($arg1) { // … return $this; } function doStuff() { // … return $this; } } Every method, that returns the object itself, can be used as

How to make chainable function in JavaScript?

邮差的信 提交于 2019-11-26 22:24:23
Lets imagine function like this: function foo(x) { x += '+'; return x; } Usage of it would be like: var x, y; x = 'Notepad'; y = foo(x); console.log(y); // Prints 'Notepad+'. I'm looking for a way to create function that's chainable with other functions. Imagine usage: var x, y; x = 'Notepad'; y = x.foo().foo().toUpperCase(); // Prints 'NOTEPAD++'. console.log(y); How would I do this? Sure, the trick is to return the object once you're done modifying it: String.prototype.foo = function() { return this + "+"; } var str = "Notepad"; console.log(str.foo().foo().toUpperCase()); http://jsfiddle.net

How to make chainable function in JavaScript?

送分小仙女□ 提交于 2019-11-26 08:19:11
问题 Lets imagine function like this: function foo(x) { x += \'+\'; return x; } Usage of it would be like: var x, y; x = \'Notepad\'; y = foo(x); console.log(y); // Prints \'Notepad+\'. I\'m looking for a way to create function that\'s chainable with other functions. Imagine usage: var x, y; x = \'Notepad\'; y = x.foo().foo().toUpperCase(); // Prints \'NOTEPAD++\'. console.log(y); How would I do this? 回答1: Sure, the trick is to return the object once you're done modifying it: String.prototype.foo