gyungsublee / nomard-typescript Goto Github PK
View Code? Open in Web Editor NEW노마드-타입스크립트-블록체인 강의
노마드-타입스크립트-블록체인 강의
개발자의 실수를 알려준다. (변수명 틀리거나, 빠진 부분이 있을 경우 등)
javascript의 단점을 보완해준다. (javascript 에서 check하지 않는 Error를 check해준다.
-> 타입 안정성 -> 코드 버그 ↓, 런타입 에러 ↓, 생산성 증가
-> why) 브라우저가 자바스크립트만 이해하기 때문이다.
-> How) 타입스크립트가 알아서 컴파일 해준다.
기존 JavaScript 런타임 (코드 실행) 에서만 확인되는 error 대신
editor에서 error를 바로 확인하여 수정하는 작업이 가능해 작업 생산성이 증가한다.
nterface, class 명
옆에 작성노마드 - 타입스크립트로 블록체인 만들기, 1 Chapter
JS는 데이터의 type을 정의하지 않는다. 따라서 데이터 타입에 대한 error를 발생시키지 않는다.
웹을 예시로 들면 설계한 api의 데이터 타입과 다른 타입의 입력값을 받아도
실행시켜주며 런타임 시 error를 발생시키지 않는다.
따라서 api/UI(User Interface) error가 발생하며
결과적으로 UX(User Experience)에 매우 좋지 않다.
클래스란 객체를 정의하고 만들어 내기 위한 설계도 혹은 틀을 말하며 클래스 안에는 객체를 만들어내기 위해 필요한 변수와 메서드들이 존재한다.
객체란 클래스에서 선언된 모양 그대로 생성된 실체를 말하며 '클래스의 인스턴스' 라고 부른다.
인스턴스란 클래스를 통해서 구현해야 할 대상(객체)이 실제로 구현된 구체적인 실체를 말한다.
붕어빵을 만들려면 붕어빵을 찍기 위한 틀이 있어야 한다.
틀이 준비되었다면 틀에 밀가루 반죽과 팥을 집어넣고 구워준다.
적당히 구워지면 틀에서 꺼낸다. 그러면 붕어빵이 만들어진다.
Class
, Object
, Instance
관점에서 살펴보면가 된다.
Class인 붕어빵틀
을 보면 아래의 그림과 같이 밀가루 반죽
과 팥
이라는 변수(member variable) 와 굽다
라는 메서드(member method) 를 가지고 있다.
이 붕어빵 틀에 밀가루와 반죽을 넣어 아래와 같이 6개의 붕어빵은 만들 때,
각 붕어빵의 반죽의 양과 팥의 양을 다르게 집어넣고 굽다()
라는 메서드를 호출하여 붕어빵을 굽는다.
이 구워지는 과정이 인스턴스화(instantiating)되고 있는 것이며
다 구워진 붕어빵들은 class를 통해 객체가 만들어 진 것이다.
각 붕어빵들은 같은 틀에서 구워졌지만 자신들만의 특징(개성)을 가지고 있다.
그 각각의 개성을 가진 붕어빵들을 인스턴스(Instance)라고 부르는 것이다.
Reference)
https://computer-science-student.tistory.com/319
https://victor8481.tistory.com/280
객체 모양을 추상화 시켜 놓은 것 으로 형태(propery, method명
과 타입
)만 정의한다. (구현 코드는 없다.)
따라서 사용 시 구현된 객체(클래스)에 할당하거나
implement
를 사용하여 shape을 체크하는데 사용한다.
객체(클래스)명: interface명
interface ExType{
ex1: string,
ex2: string,
}
const example: ExType = {
ex1: "11",
ex2: "22"
}
implement
사용: class 클래스명 implement interface명
-> implement
는 class와 class 의 interface(shape)
를 check하는데 사용함.
interface ExType{
ex1: string,
ex2: string,
}
// shape Check -> type까지 모두 구현
class example implements ExType {
constructor(
public ex1: string,
public ex2: string,
) {}
}
implement
사용 시
하여 구현함.
🤔 오버라이딩(Overriding)이란?
상위 클래스가 가지고 있는 메서드를 하위 클래스가 재정의해서 사용하는 것으로 위의 경우는 상위 클래스가
interface
로 형태(propery, method명
과타입
)만 있어 구현 코드를 작성해야 하는 것을 말한다.
get
, set
키워드아래와 같이 constructor를 통해 initialize(초기화)를 한 name
이라는 property를 가지는 Person
으로 정의된 class가 있다고 할때,
class Person {
constructor(name) {
this.name = name;
}
}
let person = new Person("John");
console.log(person.name); // John
person
이라는 instance를 만들어 person.name
를 통해 name
property에 직접적으로 접근할 수 있다.
name
property에 직접적으로 접근하지 않길 원한다면아래와 같이 getName()
, setName()
작성하여 접근한다.
class Person {
constructor(name) {
this.setName(name);
}
getName() {
return this.name;
}
setName(newName) {
newName = newName.trim();
if (newName === '') {
throw 'The name cannot be empty';
}
this.name = newName;
}
}
let person = new Person('Jane Doe');
console.log(person); // Jane Doe
person.setName('Jane Smith');
console.log(person.getName()); // Jane Smith
이러한 getName()
, setName()
메소드를 통하여 property에 접근하는 방식은 Java나 C++ 같은 다른 프로그래밍 언어에서 getter 와 setter 로 정의한다.
get
및 set
키워드를 사용하여 getter 및 setter 를 정의할 수 있다.따라서 위의 코드는 아래와 같이 표현할 수 있다.
class Person {
constructor(name) {
this.name = name;
}
get name() {
return this._name;
}
set name(newName) {
newName = newName.trim();
if (newName === '') {
throw 'The name cannot be empty';
}
this._name = newName;
}
}
(getter 와 setter의 변수명 충돌을 방지하기 위해 name
property는 _name
으로 변경함.)
let name = person.name;
person.name = 'Jane Smith';
Dos) https://www.javascripttutorial.net/es6/javascript-getters-and-setters/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.