Skip to content

Commit 8c806af

Browse files
author
Badacadabra
committed
Add Object Specifier (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent 89cd4f4 commit 8c806af

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
profile = (person) ->
2+
"""
3+
First name: #{person.firstName}
4+
Last name: #{person.lastName}
5+
Age: #{person.age}
6+
Gender: #{person.gender}
7+
Nationality: #{person.nationality}
8+
"""
9+
10+
myProfile = profile
11+
nationality: "French"
12+
gender: "M"
13+
firstName: "Baptiste"
14+
lastName: "Vannesson"
15+
age: 27
16+
17+
console.log myProfile
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var profile = function (person) {
2+
var str = "";
3+
str += "First name: " + person.firstName;
4+
str += "\nLast name: " + person.lastName;
5+
str += "\nAge: " + person.age;
6+
str += "\nGender: " + person.gender;
7+
str += "\nNationality: " + person.nationality;
8+
return str;
9+
};
10+
11+
var myProfile = profile({
12+
nationality: "French",
13+
gender: "M",
14+
firstName: "Baptiste",
15+
lastName: "Vannesson",
16+
age: 27
17+
});
18+
19+
console.log(myProfile);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const profile = ({ firstName, lastName, age, gender, nationality }) => `First name: ${firstName}
2+
Last name: ${lastName}
3+
Age: ${age}
4+
Gender: ${gender}
5+
Nationality: ${nationality}`
6+
7+
let myProfile = profile({
8+
nationality: "French",
9+
gender: "M",
10+
firstName: "Baptiste",
11+
lastName: "Vannesson",
12+
age: 27
13+
});
14+
15+
console.log(myProfile);

misc/ObjectSpecifier/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Problem
2+
3+
Functions may have a high arity, which means that they can accept a lot of arguments. This is not optimal for at least two reasons:
4+
5+
1. Keeping a nice-looking indentation is hard if the list of parameters is really long
6+
2. It is difficult for the programmer to remember the order of many positional parameters
7+
8+
# Solution
9+
10+
An object specifier (a.k.a. options object) is a simple object literal that is passed to a function instead of a long series of arguments. In other words, when a function has lots of formal parameters in its signature, it is generally a good idea to refactor it and give it only one parameter (that will be the object specifier). The good point with this pattern is that it allows named parameters instead of positional parameters, so that the order is not even a problem anymore.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
interface Person {
2+
firstName: string;
3+
lastName: string;
4+
age: number;
5+
gender: string;
6+
nationality: string;
7+
}
8+
9+
const profile = (person: Person) => `First name: ${person.firstName}
10+
Last name: ${person.lastName}
11+
Age: ${person.age}
12+
Gender: ${person.gender}
13+
Nationality: ${person.nationality}`
14+
15+
let myProfile: string = profile({
16+
nationality: "French",
17+
gender: "M",
18+
firstName: "Baptiste",
19+
lastName: "Vannesson",
20+
age: 27
21+
});
22+
23+
console.log(myProfile);

0 commit comments

Comments
 (0)