html,css,js 공부

(2024 03 11)JS 18일차

ari0930 2024. 3. 11. 23:12

(2024 03 11)JS 18일차

localStorage 

=> 데이터를 저장하는 객체

localStorage.getItem(키) = 데이터 가져오기
localStorage.setItem(키,값) =값 저장
                     .removeitem(키) = 값제거
localStorage.clear() => 모든 데이터 삭제하기

 

처음실행할때

내부에 입력하고 새로고침 눌렀을때

예외처리

         try {
                 // 예외가 발생할 수 있는 코드
                wuu.bye()
            } catch (exception) {
                console.log('예외처리')
            } finally {
                // 항상 실행되는 코드 (선택적)
                console.log("finally")
            }

 

 

객체를 생성하고 이용하는 방법

        //객체 생성
        const students=[]
        students.push({이름:'구름',국어:50,영어:70}) //필드
        students.push({이름:'별',국어:5,영어:7})
        students.push({이름:'헤릉',국어:10,영어:70})

        //출력
        alert(JSON.stringify(students,null,2))

        let ouput='이름\t총점\t평균\n'
        for(const s of students){
            const sum=s.국어+s.영어
            const average=sum/2
            ouput+=`${s.이름}\t${sum}\t${average}\n`
        }

        console.log(ouput)

        //함수를 이용한 처리 객체를 처리하는 함수는 메소드
        function sumof(students){
            return students.국어+students.영어
        }
        function averageof(students) {
            return sumof(students)/2
        }

        let out='이름\t총점\t평균\n'
        for (const s of students){
            out+=`${s.이름}\t${sumof(s)}\t${averageof(s)}\n`
        }


        console.log(out)

 

또다른 방법

        function createStduent(이름,국어,영어){
            return{이름:이름,국어:국어,영어:영어,
       
        //메소드 선언하기
            getsum () {
                return this.국어+this.영어
            },
            getAverage (){
                return this.getsum()/2
            },
            tostring() {
                return `${this.이름}\t${this.getsum()}\t${this.getAverage()}\n`
            }
        }
        }

       
        let output="이름\t국어\t영어\n"
        //객체선언
        const students=[]
        students.push(createStduent('구름',20,30))
        students.push(createStduent('별',10,20))
        students.push(createStduent('하늘',50,80))

        for( const s of students){
            output+=s.tostring()
        }
        console.log(output)

 

클래스

클래스라는 문법은 객체를 효율적으로 관리 할수 있다
추상화
프로그램에 필요한 요소만 사용해서 객체를 표현하는것 

인스턴스 :클래스 기반으로 만든객체
new 클래스 이름()

클래스 형태
class 클래스 이름 {

}

constructor 생성자
=> 클래스를 기반으로 인스턴스를 생성할때 처음  호출되는 메소드로
속성을 추가하는 등 객체의 초기화 처리

 

        //클래스 선언하기
        class Student {
            //생성자
            constructor(이름,국어,영어){
                this.이름=이름
                this.국어=국어
                this.영어=영어
            }
            getsum () {
                return this.국어+this.영어
            }
            getAverage (){
                return this.getsum()/2
            }
            tostring() {
                return `${this.이름}\t${this.getsum()}\t${this.getAverage()}\n`
            }


        }

        //학생선언하기
        const student = new Student()

        //학생 리시트를 선언하기
        const students = []
        students.push( new Student('구름',20,30))
        students.push( new Student('별',10,20))
        students.push( new Student('하늘',50,80))
        students.push( new Student('바다',40,20))

        let output="이름\t국어\t영어\n"

        for( const s of students){
            output+=s.tostring()
        }
        console.log(output)

반응형

'html,css,js 공부' 카테고리의 다른 글

(2024 03 14) js 21일차  (0) 2024.03.14
(2024 03 12) js 19일차(클래스)  (0) 2024.03.12
(2024 03 07-08)16-17일차 js  (1) 2024.03.08
(2024 03 07) js 15일차  (0) 2024.03.07
(2024 03 05) js 14일차  (0) 2024.03.05