[Javascript] js remind

2022. 12. 13. 15:22·BE
728x90

import 하는 파일에서 as 키워드를 써서 자유롭게 선택할 수 있는 별칭을 할당할 수 있음

import {hello as Hello} from './hello.js'

다수의 named export가 있는 경우 전체 내보내기도 가능

import * as bundled from './hello.js'

이 경우 나중에 접근하기 위해서는 bundeld.basedata, bundled.clean 이렇게 사용하면 된다.

 

 

class

 

class Human{
	constructor(){
    	this.gender = 'female';
    }
    printGender(){
    	console.log(this.gender);
    }
}

class Person extends Human{
	constructor(){
    	this.name = 'dropdew';
    }
    printName(){
    	console.log(this.name);
    }
}

const person = new Person();
person.printName();
person.printGender();

//error
//Must call super constructor in derived class before accessing 'this' or returning from derived constructor

 

다른 클래스를 상속(extends)받으면 상속받은 클래스 내부의 프로퍼티(constructor)와 메소드(printGender)를 사용할 수 있음

 

실행하면 오류 발생 => 서브클래스에서는 super 생성자를 먼저 호출해줘야한다 !

 

 

class Human{
	constructor(){
    	this.gender = 'female';
    }
    printGender(){
    	console.log(this.gender);
    }
}

class Person extends Human{
	constructor(){
    	super();
    	this.name = 'dropdew';
        this.gender = 'male';
    }
    printName(){
    	console.log(this.name);
    }
}

const person = new Person();
person.printName();
person.printGender();

//dropdew
//male

 

다른 클래스를 확장하고 생성자를 실행한다면, 생성자 함수 안에 super()메소드를 추가해줘야한다.

super()는 상위클래스의 생성자함수를 실행(Human 내의 constructor(){})

 

Human클래스 내의 gender는 female인데 male이 출력된 이유?

Human클래스를 상속받았지만 Person클래스에서 male로 확장되었기 때문이다.

 

생성자함수는 클래스와 비슷하고 클래스는 상속과 비슷하고 상속은 프로토타입과 비슷.. 컴포넌트 생성할 때도 이와 같은 논리

 

* 프로퍼티: 클래스와 객체에 추가되는 변수

* 메소드: 클래스와 객체에 추가되는 함수

 

//프로퍼티
//es6
constructor(){
	this.myName = 'dropdew'
}

//es7
myName = 'dropdew'

//메소드
//es6
myMethod(){}

//es7
myMethod = () => {}

 

스프레드 연산자(...)

 

const numbers = [1, 2, 3];
const newNumbers = [...numbers , 4];
const newNumbers2 = [numbers, 4];

console.log(newNumbers);
// [1 ,2, 3, 4]
console.log(newNumbers2);
// [[1, 2, 3], 4]
const person = {
	name = 'dropdew'
};

const newPerson = {
	..person,
    age : '55'
}

console.log(newPerson);

//[object Object]{
//	age: 55,
//	name: 'dropdew'
//}

이전 person객체의 모든 프로퍼티 값을 복사하기 위해 스프레드연산자(...) 사용

스프레드 연산자 사용해서 person객체를 newPerson에서 받아와서 전달

 

디스트럭처링(destructuring)

 

배열의 원소나 객체의 프로퍼티 추출해서 변수에 저장할 수 있게 함

(cf. 스프레드는 모든 원소와 프로퍼티 가져와서 새 배열이나 객체 등에 전달,

디스트럭처링은 원소나 프로퍼티 하나만 가져와서 변수에 저장)

 

const numbers = [1,2,3];
[num1, num2] = numbers;
console.log(num1, num2);
//1, 2
console.log(num1, num3);
//1, undefined
{name} = {name:'dropdew', age:'55'};
console.log(name);	//dropdew
console.log(age);	//undefined

 

참조형 및 원시형 데이터 타입

 

number, boolean, string : 기본형 자료 타입, 재할당하거나 변수를 다른 변수에 저장할 때 마다 값을 복사한다(복사본)

객체, 배열: 참조형 자료 타입

 

const num = 1;
const num2 = num;

console.log(num2); //1
const person = }
	name: 'Due'
};

const secondPerson = person;

person.name = 'dropdew';

console.log(secondPerson);

//[object Object] {name: "dropdew"}

객체를 가져다 사용하는 건 값을 복사해서 사용하는 것이 아니라,

person이 가르키는 주소값을 복사해오는 것

 

const person = }
	name: 'Due'
};

const secondPerson = {
	...person
};

person.name = 'dropdew';

console.log(secondPerson);

//[object Object] {name: "Due"}

스프레드 연산자(...)사용해서 객체를 복사해왔기 때문에 person 객체의 이름을 바꿔도 secondPerson 내의 값이 바뀌지 않는다

728x90

'BE' 카테고리의 다른 글

[Java] 오버로딩(Overloading) vs 오버라이딩(Overriding)  (2) 2024.03.12
[Java] 추상화(Abstraction)의 개념과 예제  (1) 2024.03.11
[Java] 상속(Inheritance)의 개념과 예제  (1) 2024.03.11
[Java] 캡슐화(Encapsulation)의 개념과 예제  (0) 2024.03.11
[javascript] 기본문법  (1) 2022.11.30
'BE' 카테고리의 다른 글
  • [Java] 추상화(Abstraction)의 개념과 예제
  • [Java] 상속(Inheritance)의 개념과 예제
  • [Java] 캡슐화(Encapsulation)의 개념과 예제
  • [javascript] 기본문법
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • Dev (444)
      • App·Android (1)
      • BE (50)
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (11)
        • 스프링부트 시큐리티 & JWT (0)
        • 실전 자바 기본, 중급 (1)
        • PHP (11)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (28)
        • AI (7)
        • Bigdata (6)
        • Database (1)
        • Python (0)
        • 빅데이터분석기사 (13)
      • Infra (1)
      • Activity (9)
        • Intern (0)
        • SK AI Dream Camp (2)
        • 구름톤 유니브 4기 (1)
        • 리모트 인턴십 6기 (3)
        • 봉사활동 (0)
        • 부스트캠프 AI Tech 8기 (3)
      • CS (8)
      • 취준 (13)
        • 자격증 (4)
        • 인적성·NCS (6)
        • 코테·필기·면접 후기 (3)
      • 코테 (270)
        • Algorithm (222)
        • SQL (35)
        • 정리 (13)
      • 인사이트 (27)
        • 금융경제뉴스 (7)
        • 금융용어·지식 (2)
        • 북마크 (7)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    최단경로
    다이나믹프로그래밍
    매개변수탐색
    브루트포스 알고리즘
    수학
    그리디알고리즘
    누적합
    정렬
    오블완
    구현
    티스토리챌린지
    자료구조
    그래프탐색
    투포인터
    너비우선탐색
    이분탐색
    시뮬레이션
    백준
    문자열
    그래프이론
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
DROPDEW
[Javascript] js remind
상단으로

티스토리툴바