Interface
type
abstract
abstract class User {
constructor(
protected firstName:string,
protected lastName:string
){}
abstract sayHi(name:string):string
abstract fullName():string
}
class Player extends User {
fullName() {
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string){
return `Hello ${name}. My name is ${this.fullName}`
}
}
interface
interface User {
firstName:string,
lastName:string,
sayHi(name:string):string,
fullName():string
}
class Player implements User {
constructor(
public firstName: string,
public lastName: string
){}
fullName() {
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string){
return `Hello ${name}. My name is ${this.fullName}`
}
}
챌린지 코드
interface IStorage<T> {
[key: string]: T;
}
abstract class AbstractStorage<T> {
protected storage: IStorage<T>;
constructor() {
this.storage = {};
}
abstract setItem(key: string, value: T): void;
abstract getItem(key: string): T;
abstract clearItem(key: string): void;
abstract clear(): void;
}
class LocalStorage<T> extends AbstractStorage<T> {
setItem(key: string, value: T) {
this.storage[key] = value;
}
getItem(key: string) {
return this.storage[key];
}
clearItem(key: string) {
delete this.storage[key];
}
clear() {
this.storage = {};
}
}
const aa = new LocalStorage<string>();
aa.setItem("aa", "bb");
console.log(aa.getItem("aa"));
aa.clearItem("aa");
console.log(aa.getItem("aa"));
interface IOption {
maximumAge?: number;
timeout?: number;
enableHighAccuracy?: boolean;
}
interface IGeolocationCoordinates {
readonly latitude: number;
readonly longitude: number;
readonly altitude: number | null;
readonly accuracy: number;
readonly altitudeAccuracy: number | null;
readonly heading: number | null;
readonly speed: number | null;
}
interface IGeolocationPosition {
readonly coords: IGeolocationCoordinates;
readonly timestamp: DOMHighResTimeStamp;
}
interface IGeolocationPositionError {
readonly code: number;
readonly message: string;
}
interface IGeolocation {
getCurrentPosition(
successFn: (pos: IGeolocationPosition) => void,
errorFn?: (err: IGeolocationPositionError) => void,
optionsObj?: IOption
): void;
watchPosition(
success: (pos: IGeolocationPosition) => void,
error?: (err: IGeolocationPositionError) => void,
options?: IOption
): number;
clearWatch(id: number): void;
}
class MyGeolocation<T> implements IGeolocation {
getCurrentPosition(
successFn: (pos: IGeolocationPosition) => void,
errorFn?: (err: IGeolocationPositionError) => void,
optionsObj?: IOption
): void {
navigator.geolocation.getCurrentPosition(
(position) => {
const geoPosition: IGeolocationPosition = {
coords: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
altitude: position.coords.altitude,
accuracy: position.coords.accuracy,
altitudeAccuracy: position.coords.altitudeAccuracy,
heading: position.coords.heading,
speed: position.coords.speed,
},
timestamp: position.timestamp,
};
successFn(geoPosition);
},
(error) => {
if (errorFn) {
errorFn(error);
}
},
optionsObj
);
}
watchPosition(
success: (pos: IGeolocationPosition) => void,
error?: (err: IGeolocationPositionError) => void,
options?: IOption
): number {
const watchId = navigator.geolocation.watchPosition(
(position) => {
const geoPosition: IGeolocationPosition = {
coords: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
altitude: position.coords.altitude,
accuracy: position.coords.accuracy,
altitudeAccuracy: position.coords.altitudeAccuracy,
heading: position.coords.heading,
speed: position.coords.speed,
},
timestamp: position.timestamp,
};
success(geoPosition);
},
(_error) => {
if (error) {
error(_error);
}
},
options
);
return watchId;
}
clearWatch(id: number): void {
navigator.geolocation.clearWatch(id);
}
}
const geo = new MyGeolocation();
geo.getCurrentPosition(
(position) => {
console.log("Current Position:", position);
},
(error) => {
console.error("Position Error:", error);
},
{ enableHighAccuracy: true }
);
const watchId = geo.watchPosition(
(position) => {
console.log("Watching Position:", position);
},
(error) => {
console.error("Watching Position Error:", error);
},
{ enableHighAccuracy: true }
);
geo.clearWatch(watchId);