Skip to content

Commit 9f12a59

Browse files
committed
Add interfaces and classes
1 parent 0eab732 commit 9f12a59

6 files changed

Lines changed: 113 additions & 1 deletion

File tree

typescript/classes.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
exports.__esModule = true;
18+
var Person = /** @class */ (function () {
19+
function Person(name) {
20+
this.name = name;
21+
}
22+
Person.prototype.greet = function () {
23+
console.log("Hello " + this.name);
24+
};
25+
return Person;
26+
}());
27+
var p1 = new Person("Ann Adams");
28+
p1.greet();
29+
var Employee = /** @class */ (function (_super) {
30+
__extends(Employee, _super);
31+
function Employee(name) {
32+
return _super.call(this, name) || this;
33+
}
34+
Employee.prototype.worksOn = function () {
35+
console.log(this.name + " works on many tasks.");
36+
};
37+
return Employee;
38+
}(Person));
39+
var p2 = new Employee("Mary Smith");
40+
p2.greet();
41+
p2.worksOn();

typescript/classes.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export {}
2+
3+
class Person {
4+
protected name: string
5+
6+
constructor(name: string) {
7+
this.name = name
8+
}
9+
10+
greet() {
11+
console.log(`Hello ${this.name}`)
12+
}
13+
}
14+
15+
let p1 = new Person("Ann Adams")
16+
p1.greet()
17+
18+
class Employee extends Person {
19+
constructor(name: string) {
20+
super(name)
21+
}
22+
23+
worksOn() {
24+
console.log(`${this.name} works on many tasks.`)
25+
}
26+
}
27+
let p2 = new Employee("Mary Smith")
28+
p2.greet()
29+
p2.worksOn()
30+

typescript/functions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ function add2(x, y) {
1313
}
1414
}
1515
console.log(add2(1));
16+
function add3(x, y) {
17+
if (y === void 0) { y = 10; }
18+
if (y) {
19+
return x + y;
20+
}
21+
else {
22+
return x;
23+
}
24+
}
25+
console.log(add3(2, 2));
26+
console.log(add3(1));

typescript/functions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ function add2(x: number, y?: number) : number { // y optional parameter
1212
return x
1313
}
1414
}
15-
console.log(add2(1))
15+
console.log(add2(1))
16+
17+
function add3(x: number, y: number = 10) : number { // y default value
18+
if (y) {
19+
return x + y
20+
} else {
21+
return x
22+
}
23+
}
24+
console.log(add3(2,2))
25+
console.log(add3(1))

typescript/interfaces.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
function printFullName(person) {
4+
console.log(person.firstName + " " + person.lastName);
5+
}
6+
printFullName({ firstName: 'Joe', lastName: 'Doe' });
7+
printFullName({ firstName: 'Mary' });

typescript/interfaces.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export {}
2+
3+
interface Person {
4+
firstName: string,
5+
lastName?: string
6+
}
7+
8+
function printFullName(person: Person) {
9+
console.log(`${person.firstName} ${person.lastName}`)
10+
}
11+
12+
printFullName({firstName:'Joe', lastName:'Doe'})
13+
printFullName({firstName:'Mary'})

0 commit comments

Comments
 (0)