3. 데이터 타입(bigInt, symbol), Dynamic Typing 때문에 탄생한 TypeScript, constants 안의 값은 변경 가능

jean·2021년 2월 7일
0
  1. 글로벌 변수는 프로그램 시작부터 끝까지 메모리에 탑재돼 있기 때문에 최소한으로 쓰는 게 좋다.

  2. var(old): 선언 전에 값을 할당하거나 출력할 수 있다.
    var (don't ever use this!)
    var hoisting (move declaration from bottom to top)
    has no block scope

let(added in ES6): 인터넷 익스플로러에서 지원 X, 하지만 사용률 1%

(BABEL을 이용해 ES6로 개발하고 배포 시 ES5로 해도 된다.)

Constants : immutable data type

let : mutable data type

favor immutable data type always for a few reasons:
- security
- thred safety (다양한 스레드가 동시에 값을 변경하는 것을 막기 위해)
- reduce human mistakes

  1. primitive(single item): number, string, boolean, null, undefined, symbol
    object(싱글 아이템을 하나의 단위로 묶어놓은 것)
    function(first-class function: 함수를 변수에 할당할 수 있으므로, 함수를 함수의 인자로 전달하거나 함수를 리턴할 수 있다.)

  2. JavScript에서는 숫자 타입이 하나, number 뿐!
    let a = 12;
    let b = 1.2;

*TypeScript에서는 number 키워드를 써줘야 한다.

let a: number = 12;
et b: number = 1.2;

6. JavScript의 특별한 number values

const infinity = 1 / 0; // Infinity
const negativeInfinity = -1 / 0 // -Infinity
const nAn = 'string' / 2 // NaN

  1. 사파리에서 지원 안 되는 최신 기능(숫자 뒤에 n입력 시 bigInt로 타입이 변경됨)
    const int = 1; typeof int => number
    const bigInt = 1n; typeof bigInt => bigInt

  2. template literal
    string ${value}
    "string" + value

  3. false = 0, null, undefined, NaN, ''
    true = 1 and otehr values

  4. null
    let nothing = null;

  5. undefined
    let x;

12. Symbol('값'); => 값에 상관 없이 고유한 식별자가 필요할 때 사용

const symbol1 = Symbol('string');
const symbol2 = Symbol('string');
symbol1 === symbol2 // false

13. Symbol.for('값'); => 값이 같다면 같은 심볼을 만들고 싶을 때 사용

const gSymbol1 = Symbol.for('string');
const gSymbol2 = Symbol.for('string');
gSymbol1 === gSymbol2 // true

14. symbolname.description => 심볼 안의 값 출력하고 싶을 때 꼭 .description 사용

Dynamic Typing

JavaScript: Dynamically typed language (런타임에 할당된 값에 따라 타입이 변경될 수 있음, 유연하지만 큰 프로젝트 개발 시 문제가 있음)
Java, C: Statically typed language (변수 선언 시 어떤 타입인지 결정)

TypeScript

  1. 이렇게 변수값이 런타임에 할당된 값에 따라 변할 수 있는 유연성 때문에 TypeScript가 탄생
    (자바스크립트위에 타입이 올려진 언어)

자바스크립트는 브라우저가 이해할 수 있는 언어기 때문에 실시간으로 연동해서 볼 수 있지만, 타입스크립트는 브라우저가 이해할 수 있는 언어로 trans compiler를 이용해야 되기 때문에 (BABEL),
자바스크립트를 먼저 배운 후 나중에 빠르게 TypeScript를 배우면 된다.

18. const object 안의 값은 변경할 수 있다.

const ellie = { name: 'ellie', age: 20 };

ellie의 값은 변경할 수 없지만
ellie.name이나 ellie.age의 값은 변경할 수 있다.

0개의 댓글