Skip to content

Commit 3f1c16c

Browse files
committed
Complete TS classes lesson
1 parent 70c777d commit 3f1c16c

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var Customer = /** @class */ (function () {
2+
function Customer(theFirst, theLast) {
3+
this.firstName = theFirst;
4+
this.lastName = theLast;
5+
}
6+
return Customer;
7+
}());
8+
var myCustomer = new Customer("Martin", "Dixon");
9+
console.log(myCustomer.firstName);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Customer {
2+
3+
private firstName: string;
4+
private lastName: string;
5+
6+
constructor(theFirst: string, theLast: string) {
7+
this.firstName = theFirst;
8+
this.lastName = theLast;
9+
}
10+
}
11+
12+
let myCustomer = new Customer("Martin", "Dixon");
13+
14+
// npx tsc --noEmitOnError 01-class-definition/Customer.ts will keep it from compiling when there is an error.
15+
// if firstName is private it will error out but it compiles anyway unless we flag it as explained above.
16+
console.log(myCustomer.firstName)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Customer1 = /** @class */ (function () {
2+
function Customer1(theFirst, theLast) {
3+
this._firstName = theFirst;
4+
this._lastName = theLast;
5+
}
6+
Object.defineProperty(Customer1.prototype, "firstName", {
7+
get: function () {
8+
return this._firstName;
9+
},
10+
set: function (value) {
11+
this._firstName = value;
12+
},
13+
enumerable: false,
14+
configurable: true
15+
});
16+
Object.defineProperty(Customer1.prototype, "lastName", {
17+
get: function () {
18+
return this._lastName;
19+
},
20+
set: function (value) {
21+
this._lastName = value;
22+
},
23+
enumerable: false,
24+
configurable: true
25+
});
26+
return Customer1;
27+
}());
28+
// let's create an instance
29+
var myCustomer1 = new Customer1("Martin", "Dixon");
30+
myCustomer1.firstName = "Susan";
31+
myCustomer1.lastName = "Public";
32+
console.log(myCustomer1.firstName);
33+
console.log(myCustomer1.lastName);
34+
// For it to compile with this syntax we have to provide the flag --target ES5
35+
// tsc --target ES5 --noEmitOnError Customer.ts
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Customer1 {
2+
3+
private _firstName: string;
4+
private _lastName: string;
5+
6+
constructor(theFirst: string, theLast: string) {
7+
this._firstName = theFirst;
8+
this._lastName = theLast;
9+
}
10+
11+
public get firstName(): string {
12+
return this._firstName;
13+
}
14+
15+
public set firstName(value: string) {
16+
this._firstName = value;
17+
}
18+
19+
public get lastName(): string {
20+
return this._lastName;
21+
}
22+
public set lastName(value: string) {
23+
this._lastName = value;
24+
}
25+
26+
}
27+
28+
// let's create an instance
29+
let myCustomer1 = new Customer1("Martin", "Dixon");
30+
31+
myCustomer1.firstName = "Susan";
32+
myCustomer1.lastName = "Public";
33+
34+
console.log(myCustomer1.firstName);
35+
console.log(myCustomer1.lastName);
36+
37+
// For it to compile with this syntax we have to provide the flag --target ES5
38+
// tsc --target ES5 --noEmitOnError Customer.ts
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Customer2 {
2+
constructor(
3+
private _firstName: string,
4+
private _lastName: string,
5+
) {}
6+
7+
public get firstName(): string {
8+
return this._firstName;
9+
}
10+
11+
public set firstName(value: string) {
12+
this._firstName = value;
13+
}
14+
15+
public get lastName(): string {
16+
return this._lastName;
17+
}
18+
public set lastName(value: string) {
19+
this._lastName = value;
20+
}
21+
}
22+
23+
// let's create an instance
24+
let myCustomer2 = new Customer2("Martin", "Dixon");
25+
26+
myCustomer2.firstName = "Susan";
27+
myCustomer2.lastName = "Public";
28+
29+
console.log(myCustomer2.firstName);
30+
console.log(myCustomer2.lastName);
31+
32+
// For it to compile with this syntax we have to provide the flag --target ES5
33+
// tsc --target ES5 --noEmitOnError Customer.ts
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Customer3 {
2+
constructor(
3+
private _firstName: string,
4+
private _lastName: string,
5+
) {}
6+
7+
public get firstName(): string {
8+
return this._firstName;
9+
}
10+
11+
public set firstName(value: string) {
12+
this._firstName = value;
13+
}
14+
15+
public get lastName(): string {
16+
return this._lastName;
17+
}
18+
public set lastName(value: string) {
19+
this._lastName = value;
20+
}
21+
}
22+
23+
export default Customer3;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Customer3 from "./Customer";
2+
// let's create an instance
3+
let myCustomer3 = new Customer3("Martin", "Dixon");
4+
5+
myCustomer3.firstName = "Susan";
6+
myCustomer3.lastName = "Public";
7+
8+
console.log(myCustomer3.firstName);
9+
console.log(myCustomer3.lastName);
10+
11+
// For it to compile with this syntax we have to provide the flag --target ES5
12+
// tsc --target ES5 --noEmitOnError Customer.ts

typescript-training/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
// "newLine": "crlf", /* Set the newline character for emitting files. */
6868
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
6969
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
70-
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
70+
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
7171
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
7272
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
7373

0 commit comments

Comments
 (0)