Skip to content

Commit fa55a0c

Browse files
author
Badacadabra
committed
Add Method Chaining (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent 2da903a commit fa55a0c

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
person =
2+
setFirstName: (@firstName = "John") -> @
3+
setLastName: (@lastName = "Doe") -> @
4+
sayHello: ->
5+
if !@firstName or !@lastName
6+
throw
7+
type: "No data"
8+
message: "A person needs a first name and a last name before to say hello."
9+
"Hello, #{this.firstName} #{this.lastName}!"
10+
11+
try
12+
console.log person.setFirstName("Baptiste").setLastName("Vannesson").sayHello() # Here is the chain!
13+
catch e
14+
console.log #{e.type} - #{e.message}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var person = {
2+
setFirstName: function (firstName) {
3+
this.firstName = firstName || "John";
4+
return this;
5+
},
6+
setLastName: function (lastName) {
7+
this.lastName = lastName || "Doe";
8+
return this;
9+
},
10+
sayHello: function () {
11+
if (!this.firstName || !this.lastName) {
12+
throw {
13+
type: "No data",
14+
message: "A person needs a first name and a last name before to say hello."
15+
};
16+
}
17+
return "Hello, " + this.firstName + " " + this.lastName + "!";
18+
}
19+
};
20+
21+
try {
22+
console.log(person.setFirstName("Baptiste").setLastName("Vannesson").sayHello()); // Here is the chain!
23+
} catch (e) {
24+
console.log(e.type + " - " + e.message);
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const person = {
2+
setFirstName(firstName = "John") {
3+
this.firstName = firstName;
4+
return this;
5+
},
6+
setLastName(lastName = "Doe") {
7+
this.lastName = lastName;
8+
return this;
9+
},
10+
sayHello() {
11+
if (!this.firstName || !this.lastName) {
12+
throw {
13+
type: "No data",
14+
message: "A person needs a first name and a last name before to say hello."
15+
};
16+
}
17+
return `Hello, ${this.firstName} ${this.lastName}!`;
18+
}
19+
};
20+
21+
try {
22+
console.log(person.setFirstName("Baptiste").setLastName("Vannesson").sayHello()); // Here is the chain!
23+
} catch (e) {
24+
console.log(e.type + " - " + e.message);
25+
}

misc/MethodChaining/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Problem
2+
3+
In a program, we may have a lot of function calls on the same object. This often leads to repetition because it is necessary to write the reference again and again.
4+
5+
# Solution
6+
7+
Method Chaining allows us to make multiple calls on the same object without repeating ourselves. This pattern is very easy to use because the only thing to do is to return the current object ("this") in all methods that are supposed to be "chainable".
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface Person {
2+
setFirstName(firstName: string): Person;
3+
setLastName(lastName: string): Person;
4+
sayHello(): string | never;
5+
}
6+
7+
const person: Person = {
8+
setFirstName(firstName: string = "John"): Person {
9+
this.firstName = firstName;
10+
return this;
11+
},
12+
setLastName(lastName: string = "Doe"): Person {
13+
this.lastName = lastName;
14+
return this;
15+
},
16+
sayHello(): string | never {
17+
if (!this.firstName || !this.lastName) {
18+
throw {
19+
type: "No data",
20+
message: "A person needs a first name and a last name before to say hello."
21+
};
22+
}
23+
return `Hello, ${this.firstName} ${this.lastName}!`;
24+
}
25+
};
26+
27+
try {
28+
console.log(person.setFirstName("Baptiste").setLastName("Vannesson").sayHello()); // Here is the chain!
29+
} catch (e) {
30+
console.log(e.type + " - " + e.message);
31+
}

0 commit comments

Comments
 (0)