File: controlFlowInstanceOfGuardPrimitives.js

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (30 lines) | stat: -rw-r--r-- 964 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//// [controlFlowInstanceOfGuardPrimitives.ts]
function distinguish(thing: string | number | Date) {
    if (thing instanceof Object) {
        console.log("Aha!! It's a Date in " + thing.getFullYear());
    } else if (typeof thing === 'string') {
        console.log("Aha!! It's a string of length " + thing.length);
    } else {
        console.log("Aha!! It's the number " + thing.toPrecision(3));
    }
}

distinguish(new Date());
distinguish("beef");
distinguish(3.14159265);

//// [controlFlowInstanceOfGuardPrimitives.js]
function distinguish(thing) {
    if (thing instanceof Object) {
        console.log("Aha!! It's a Date in " + thing.getFullYear());
    }
    else if (typeof thing === 'string') {
        console.log("Aha!! It's a string of length " + thing.length);
    }
    else {
        console.log("Aha!! It's the number " + thing.toPrecision(3));
    }
}
distinguish(new Date());
distinguish("beef");
distinguish(3.14159265);