1class Person {
2 constructor(fname, lname) {
3 this.firstName = fname;
4 this.lastName = lname;
5 }
6}
7
8const person = new Person('testFirstName', 'testLastName');
9
10console.log(person.firstName); // testFirstName
11console.log(person.lastName); // testLastName
12
13
1const personFactory = (firstName, lastName, email) => {
2 return {
3 firstName: firstName,
4 lastName: lastName,
5 email: email,
6 info() {
7 return `${this.firstName} ${this.lastName}, ${this.email}`;
8 }
9 };
10};
11
12let person = personFactory('John', 'Doe', 'jdoe@example.com');
13
14console.log(person.info());
15
1let Person = function (firstName, lastName, email) {
2
3 this.firstName = firstName;
4 this.lastName = lastName;
5 this.email = email;
6}
7
8let PersonBuilder = function () {
9
10 let firstName;
11 let lastName;
12 let email;
13
14 return {
15 setFirstName: function (firstName) {
16 this.firstName = firstName;
17 return this;
18 },
19 setLastName: function (lastName) {
20 this.lastName = lastName;
21 return this;
22 },
23 setEmail: function (email) {
24 this.email = email;
25 return this;
26 },
27 info: function () {
28 return `${this.firstName} ${this.lastName}, ${this.email}`;
29 },
30 build: function () {
31 return new Person(firstName, lastName, email);
32 }
33 };
34};
35
36var person = new PersonBuilder().setFirstName('John').setLastName('Doe')
37 .setEmail('jdoe@example.com');
38console.log(person.info());
39
1function Person(fname, lname) {
2 this.firstName = fname;
3 this.lastName = lname;
4}