Skip to content

Instantly share code, notes, and snippets.

@DonHuskini
Created July 19, 2022 19:24
Show Gist options
  • Save DonHuskini/ba648d7285ed39fc95e97f127c36c04d to your computer and use it in GitHub Desktop.
Save DonHuskini/ba648d7285ed39fc95e97f127c36c04d to your computer and use it in GitHub Desktop.
User following/followers
class User {
id = undefined;
username = undefined;
following = [];
followers = [];
constructor({ id, username }) {
this.id = id;
this.username = username;
}
follow(user) {
if (this === user) throw Error("Cannot follow yourself");
this.following.push(user);
user.followers.push(this);
}
get following() {
return this.following;
}
get followers() {
return this.followers;
}
}
const user1 = new User({ id: 1, username: "foo" });
const user2 = new User({ id: 3, username: "bar" });
const user3 = new User({ id: 4, username: "doo" });
const user4 = new User({ id: 5, username: "wee" });
const user5 = new User({ id: 6, username: "buz" });
user1.follow(user2);
user1.follow(user4);
user1.follow(user5);
user2.follow(user1);
user3.follow(user2);
user3.follow(user5);
user5.follow(user2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment