Week 4 - JavaScript

김우진·2022년 9월 5일
  • 객체란 무엇이며 필요한 이유

    하나의 변수에 여러 속성을 저장할 수 있도록 해주는 자바스크립트의 참조형 데이터 타입 중 하나
    아래 이미지처럼 Key(속성 이름)과 Value(속성 값)이 한 쌍의 Property(속성)으로 연결되어 있으며, 배열과 달리 순서가 중요하지 않다.

    자바스크립트는 객체(Object) 기반의 스크립트 언어이며, 자바스크립트를 이루고 있는 거의 "모든 것"이 객체다. 원시 타입(Primitives)을 제외한 나머지 값들(함수, 배열, 정규표현식 등)은 모두 객체이기 때문에 자바스크립트에서 객체는 필수 불가결한 존재다.

  • 객체에서 property, key, value

    객체는 property의 집합이며, 프로퍼티는 키와 값으로 구성된다.
    property를 나열할 때는 쉼표(,)로 구분한다. 일반적으로 마지막 프로퍼티 뒤에는 쉼표를 사용하지 않으나 사용해도 좋다.
    property key와 property value로 사용할 수 있는 값은 다음과 같다.

    property key : 빈 문자열을 포함하는 모든 문자열 또는 심벌 값
    property value : 자바스크립트에서 사용할 수 있는 모든 값

  • 객체에 접근 하는 두 가지가 있는 이유

    Dot Notation
    Dot notation is the most common way to access elements in JavaScript. To use dot notation, you simply write the name of the object followed by a dot and the name of the property you want to access. For example, if we have an object called “person” with a property called “name”, we would access the name property using person.name.

    Bracket Notation
    Bracket notation is less common than dot notation, but it is useful in certain situations. To use bracket notation, you write the name of the object followed by brackets and the property you want to access. For example, if we have an object called “person” with a property called “name”, we would access the name property using person[“name”].

    dot notation is the most common way to access elements in JavaScript. However, there are certain situations where bracket notation is more appropriate. When you want to use a variable to access a property, or if a property has special characters in its name, you should use bracket notation.

  • 객체의 값을 추가,수정, 삭제하는 방법

    객체의 속성을 삭제할 때는 delete 연산자를 사용.
    dot notation, bracket notation 모두 사용 할 수 있음.

    delete obj.id;
    delete obj.product['product type']);

  • 객체와 배열이 섞인 복잡한 객체 만들어서 접근하는 방법

    	let myPlants = [
    	   {
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
      },
      {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
        ]
    	   }
    	  ];
    
    	let foundValue = myPlants[1].list[1]; // let foundValue = myPlants[1]['list'][1]
    	console.log(foundValue); //"pine"
  • 배열의 타입이 객체인 이유

    배열은 JavaScript의 원시 자료형이 아닌 객체형에 속하기 때문에 객체처럼 동작한다.

0개의 댓글