공부하기

부족한 점 공부하기3

Hyeon been 2023. 3. 12. 14:26

한번씩 읽고 가세요.

“ 지연되는 프로젝트에 인력을 더 투입하면 오히려 더 늦어진다. ”

- Frederick Philips Brooks
Mythical Man-Month 저자
728x90

부족한 점 공부하기

forEach

forEach( )문은 배열의 데이터가 있을 경우 사용합니다.
forEach( )문 안에는 함수가 들어가는 콜백 함수입니다.
forEach( )문은 element(값), index(인덱스), array(배열)을 불어오는 특징이 있습니다.

 

{
    const num = [100, 200, 300, 400, 500];

    num.forEach(function(element, index, array){
        document.write(element);
        document.write(index);
        document.write(array);
    });
}

 

hasOwnProperty( )

 

객체에 특정 속성이 존재하는지 여부를 나타내는 불리언 값을 반환합니다.
해당 속성이 객체 자신의 속성으로 존재하면 true를 반환하고, 해당 속성이 존재하지 않으면 false를 반환합니다.

{
    const obj = {
        a: 100,
        b: 200,
        c: "javascript"
    };

    console.log(obj.hasOwnProperty("a"));       // true
    console.log(obj.hasOwnProperty("b"));       // true
    console.log(obj.hasOwnProperty("c"));       // true
    console.log(obj.hasOwnProperty("d"));       // false      
}

 

생성자 함수

생성자 함수의 규칙은 함수 이름의 첫 글자는 대문자, 반드시 'new' 연산자를 붙여 함수(인스턴스 생성)를 실행합니다.

{
    function Func(num, name, word){
        this.num = num;
        this.name = name;
        this.word = word;
        this.result = function( ){
            document.write(this.num + ". " + this.name + "가 " + this.word + "되었습니다.");
        }
    };

    // 인스턴스 생성
    const info1 = new Func(1, "함수", "실행");     
    const info2 = new Func(2, "자바스크립트", "실행");
    const info3 = new Func(3, "리액트", "실행");

    info1.result( );
    info2.result( );
    info3.result( );
}

 

프로토 타입 함수

prototype 함수는 자바스크립트에서 모든 객체가 가지는 속성(property)으로, 객체의 원형(prototype object)을 가리킵니다.
객체의 프로토타입은 해당 객체를 생성하는데 사용된 생성자 함수의 prototype 속성으로 설정됩니다.
즉, 생성자 함수를 사용하여 객체를 생성하면 해당 객체는 생성자 함수의 prototype 객체를 상속하게 되고, 이 prototype 객체의 속성과 메서드를 해당 객체에서 사용할 수 있게 됩니다.
이처럼 prototype을 사용하면 객체의 속성과 메서드를 공유하여 메모리 사용량을 줄이고 코드의 재사용성을 높일 수 있습니다.

 

{
    function Func(num, name, word){
        this.num = num;
        this.name = name;
        this.word = word;
    };

    Func.prototype.result = function( ){
        document.write(this.num + ". " + this.name + "가 " + this.word + "되었습니다.");
    };

    const info1 = new Func(1, "함수", "실행");     
    const info2 = new Func(2, "자바스크립트", "실행");
    const info3 = new Func(3, "리액트", "실행");

    info1.result( );
    info2.result( );
    info3.result( );
}