/* 자바스크립트 공부 DAY-01 (2022.10.17) */

// Date함수 사용하기~ **new로 선언해야함!!
const today = new Date();
console.log("fullYear", today.getFullYear());
// 출력결과: 'fullYear' 2022
const month = today.getMonth()+1;
console.log("month", month);
// 출력결과: 'month' 10
console.log("date", today.getDate());
// 출력결과: 'date' 17
console.log("today", today);
// 출력결과: 'today' 2022-10-17T09:59:32.358Z
console.log("today -> toString", today.toString());
// 출력결과: 'today -> toString' 'Mon Oct 17 2022 18:59:32 GMT+0900 (한국 표준시)'
console.log("today -> toDateString", today.toDateString());
// 출력결과: 'today -> toDateString' 'Mon Oct 17 2022'
console.log("today -> toLocaleDateString", today.toLocaleDateString());
// 출력결과: 'today -> toLocaleDateString' '2022. 10. 17.'

/* ----------------------------------------------------------------------------------------- */


// 값에 의한 복사
const valueA = "aa";
const valueB = valueA;
// 실제로 값이 복사되어 원본과 별개로 다른 결과를 볼 수 있다.
console.log(valueB + "AA");
// 출력결과: 'aaAA'
console.log(valueA);
// 출력결과: 'aa'

/* ----------------------------------------------------------------------------------------- */

// 참조에 의한 복사 -> 값 자체가 할당되는 것이 아닌, 원본의 값을 가리키는 것
const xmas = new Date(2022, 12, 25);
const solstics = xmas;
console.log("xmas", xmas.getDate())
// 출력결과: 'xmas' 25
console.log("solstics.setDate(21)", solstics.setDate(21));
// 출력결과: 'solstics.setDate(21)' 1674226800000
// solostics의 값을 변경하였는데, xmas의 값까지 변한 결과를 볼 수 있다.
// 값에 의한 복사가 아닌 참조에 의한 복사이기 때문이다. (**객체는 참조에 의한 복사가 된다!!)
console.log("xmas", xmas.getDate());
// 출력결과: 'xmas' 21

/* ----------------------------------------------------------------------------------------- */

// 변수
// 변수를 한번에 여러개 선언
var a, b;
a = "a";
b = "b";
console.log("a = " + a, ", b = " + b);
// 출력결과: 'a'

// 변수의 유효범위 (전역변수, 지역변수)
const scope = "global"
function checkScope(){
  const scope = "local";
  console.log("in function scope", scope);
  // 출력결과: 'b'
}
// 함수 안과 밖에서 선언한, 같은 이름의 변수의 값이 다르게 나온다
checkScope();
console.log("in global scope", scope);
// 출력결과: 'a = a' ', b = b'

/* ----------------------------------------------------------------------------------------- */

/*  참조타입인 객체와 배열은 어떤 크기라도(크기가 정해져 있지 않음) 가질 수 있다.
    이와 유사하게 함수도 포함할 수 있으며, 참조타입은 8byte 메모리를 가지는 변수에 직접 저장 할 수 없다.
    대신 변수에 저장되는 것은 이러한 값에 대한 '참조'일 뿐이다. 
    참조는 데이터 값 자체가 아니라 값을 찾을 수 있는 위치를 변수에게 알려주는 것이다.
*/
const arrayA = [1, 2, 3];
const arrayB = arrayA;
arrayA[0] = 100;
console.log("arrayA", arrayA)
// 출력결과: 'in function scope' 'local'
console.log("arrayB", arrayB)
// 출력결과: 'in global scope' 'global'

** 콘솔 결과도 나중에 적어놓자... (+업데이트 완료)

profile
개인 이력, 포폴 관리 및 기술 블로그 사이트 👉 https://aimzero-web.vercel.app/

0개의 댓글