mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
Step 2-01 updates
This commit is contained in:
@@ -3,10 +3,10 @@ async function fetchSomething() {
|
||||
return await response.text();
|
||||
}
|
||||
|
||||
// Async functions always returns Promise
|
||||
// Async functions always return a Promise
|
||||
fetchSomething().then(text => {
|
||||
console.log('hello ' + text);
|
||||
});
|
||||
|
||||
// adding an export to turn this into a "module"
|
||||
// adding an export turns this into a "module"
|
||||
export default {};
|
||||
|
||||
@@ -19,5 +19,5 @@ function reverse<T>(arg: T[]): T[] {
|
||||
return arg;
|
||||
}
|
||||
|
||||
// adding an export to turn this into a "module"
|
||||
// adding an export turns this into a "module"
|
||||
export default {};
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
// Interface for an object or class
|
||||
interface Car {
|
||||
make: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
class MyCar implements Car {
|
||||
make: 'Honda';
|
||||
model: 'Accord';
|
||||
}
|
||||
make: string;
|
||||
model: string;
|
||||
|
||||
const myCar: Car = {
|
||||
constructor(make: string, model: string) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
const myCar1: Car = new MyCar('Honda', 'Accord');
|
||||
|
||||
const myCar2: Car = {
|
||||
make: 'Honda',
|
||||
model: 'Accord'
|
||||
};
|
||||
|
||||
// Interface as Functions
|
||||
// Interface for a function
|
||||
interface InterestingFn {
|
||||
(someArgs: string): number;
|
||||
}
|
||||
const interesting: InterestingFn = (someArgs: string): number => {
|
||||
return Number(someArgs);
|
||||
};
|
||||
|
||||
// adding an export to turn this into a "module"
|
||||
export default {};
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// These are named imports from a file relative to this file
|
||||
import { namedConst, namedFn, namedObj, namedConstBracket } from './named';
|
||||
|
||||
// We can even apply an alias to the named constant
|
||||
// We can even give an alias to the named constant
|
||||
import { namedConst as c } from './named';
|
||||
|
||||
// These are the same instances of the named imports, but gets imported all at the same time under a single object
|
||||
// These are the *same instances* of the named imports, but they all get imported inside a single object
|
||||
import * as named from './named';
|
||||
|
||||
// Print out the exports
|
||||
@@ -14,12 +14,15 @@ console.log(namedFn());
|
||||
console.log(namedObj);
|
||||
console.log(namedConstBracket);
|
||||
|
||||
// Print out exports through module level import
|
||||
// Print out exports from module level import
|
||||
console.log(named.namedConst);
|
||||
console.log(named.namedFn());
|
||||
console.log(named.namedObj);
|
||||
console.log(named.namedConstBracket);
|
||||
|
||||
// The named and module-level imports reference the same object instances
|
||||
console.log(namedObj === named.namedObj); // true
|
||||
|
||||
// Default import can be named anything we want as the consumer
|
||||
import DefaultClass from './default';
|
||||
import Foo from './default';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Destructuring
|
||||
var [a, b, ...rest] = [1, 2, 3, 4];
|
||||
console.log(a, b, rest); // 1,2,[3,4]
|
||||
console.log(a, b, rest); // 1 2 [3,4]
|
||||
|
||||
// Array assignment
|
||||
var list = [1, 2];
|
||||
@@ -20,5 +20,5 @@ const obj3 = { ...obj1, ...obj2 };
|
||||
// Destructuring object
|
||||
const { x } = obj3;
|
||||
|
||||
// adding an export to turn this into a "module"
|
||||
// adding an export turns this into a "module"
|
||||
export default {};
|
||||
|
||||
@@ -47,13 +47,18 @@ let choose1 = <Specific1>{ common: '5' };
|
||||
type CatStatus = 'alive' | 'dead' | 'both';
|
||||
|
||||
// Classes
|
||||
class Animal {}
|
||||
class Animal { }
|
||||
|
||||
// Illustration purposes only
|
||||
// In real apps, avoid inheritance if possible
|
||||
// noted exception: React.Component with react@<16.8.0
|
||||
class Cat extends Animal {}
|
||||
class Dog extends Animal {}
|
||||
class Cat extends Animal { }
|
||||
class Dog extends Animal { }
|
||||
|
||||
// adding an export to turn this into a "module"
|
||||
// Any Type - avoid if possible
|
||||
let mystery: any = "I don't like the person who will be maintaining this code";
|
||||
mystery = 2;
|
||||
mystery = () => 3;
|
||||
|
||||
// adding an export turns this into a "module"
|
||||
export default {};
|
||||
|
||||
Reference in New Issue
Block a user