If I have pseudo code like :
function user(a,b)
{
if(! (this instanceof user) ) return new user(a,b);
this.a = a;
this.b = b;
this.showName = function() {
alert(this.a + " " + this.b);
};
this.changeName = function(a,b) {
this.a = a;
this.b = b;
};
}
I can call it like :
user("John", "Smith").showName() // output : John Smith
I want something like :
user("John", "Smith").changeName("Adam", "Smith").showName();
Return the object in every method. This is called "chaining".
function user(a,b)
{
if(! (this instanceof user) ) return new user(a,b);
this.a = a;
this.b = b;
this.showName = function() {
alert(this.a + " " + this.b);
return this; // <--- returning this
};
this.changeName = function(a,b) {
this.a = a;
this.b = b;
return this; // <--- returning this
};
}
DEMO: http://jsbin.com/oromed/
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