Skip to content

Commit 9fb0704

Browse files
author
a.cheprasov
committed
Lesson 10: Function signatures
1 parent 73bd37b commit 9fb0704

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

public/sandbox.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
"use strict";
2+
// let greet: Function;
3+
// example 1
24
var greet;
3-
greet = function () {
4-
console.log('hello again, world');
5+
greet = function (name, greeting) {
6+
console.log("".concat(name, " says ").concat(greeting));
57
};
6-
var add = function (a, b, c) {
7-
if (c === void 0) { c = 10; }
8-
console.log(a + b);
9-
console.log(c);
8+
// example 2
9+
var calc;
10+
calc = function (numOne, numTwo, action) {
11+
if (action === 'add') {
12+
return numOne + numTwo;
13+
}
14+
else {
15+
return numOne - numTwo;
16+
}
1017
};
11-
add(5, 10, 20);
12-
var minus = function (a, b) {
13-
return a + b;
18+
// example 3
19+
var logDetails;
20+
logDetails = function (ninja) {
21+
console.log("".concat(ninja.name, " is ").concat(ninja.age, " years old"));
1422
};
15-
var result = minus(10, 7);

src/sandbox.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
let greet: Function;
1+
// let greet: Function;
22

3-
greet = () => {
4-
console.log('hello again, world');
3+
// example 1
4+
let greet: (a: string, b: string) => void;
5+
greet = (name: string, greeting: string) => {
6+
console.log(`${name} says ${greeting}`)
57
}
68

7-
const add = (a: number, b: number, c: number | string = 10): void => {
8-
console.log(a + b);
9-
console.log(c);
9+
// example 2
10+
let calc: (a: number, b: number, c: string) => number;
11+
12+
calc = (numOne: number, numTwo: number, action: string) => {
13+
if (action === 'add') {
14+
return numOne + numTwo;
15+
} else {
16+
return numOne - numTwo;
17+
}
1018
}
1119

12-
add(5, 10, 20);
20+
// example 3
21+
let logDetails: (obj: {name: string, age: number}) => void;
1322

14-
const minus = (a: number, b: number) => {
15-
return a + b;
16-
}
23+
type person = {name: string, age: number};
1724

18-
let result = minus(10, 7);
25+
logDetails = (ninja: person) => {
26+
console.log(`${ninja.name} is ${ninja.age} years old`);
27+
}

0 commit comments

Comments
 (0)