I am trying to understand is how to create call methods after a method call.
For example in jquery you have something like this:
$("blah").data("data-id");
How would I make :
blah("cow").foo("moo");
Where the mothods blah and foo just console.log(value)?
What you're referring to is a "fluent API" (also calling "chaining"). Your functions need to return the object that has the next method you want to call on it. For example,
var obj = function(){
var self = this;
self.blah = function(v){ console.log(v); return self; };
self.foo = function(v){ console.log(v); return self; };
};
var o = new obj();
o.blah("cow").foo("moo");
See this article for more info: http://www.i-programmer.info/programming/javascript/4676-chaining-fluent-interfaces-in-javascript.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With