객체 생성자 함수

내장 객체를 생성할 때는 이미 자바스크립트 연산에 내장되어 있는 객체 생성자 함수를 사용하여 객체를 생성합니다.

객체 생성자 함수

function 함수명(매개변수1, 매개변수2,......){ this.속성명 = 새 값; this.함수명 = function(){ //자바스크립트 실행 코드 } } let 참조 변수(인스턴스 네임) = new 함수명(); //객체 생성 let 참조 변수 = {속성: 새 값, 함수명 : function(){........}}

function obj5(a,b){
        this.a = a;
        this.b = b;
        this.c = function(){
            return a * b;
        }
}
let result1 = new obj5(100, 200);
let result2 = new obj5("자바스크립트", "실행했습니다.");
document.write(result1.a);
document.write(result1.b);
document.write(result1.c());
document.write(result2.a);
document.write(result2.b);

키와 몸무게로 평균 구하기

function CheckWeight(name, height, weight){
    this.useName = name;
    this.useHeight = height;
    this.useWeight = weight;
    this.minWeight;
    this.maxWeight;
    this.getInfo = function(){
        let str = "";
        str += "이름: " + this.useName + ", ";
        str += "키:" + this.useHeight + ", ";
        str += "몸무게:" + this.useWeight + ", ";
        return str;

    }
    this.getResult = function(){
        this.minWeight = (this.useHeight - 100) * 0.9 - 5;
        this.maxWeight = (this.useHeight - 100) * 0.9 + 5;

        if(this.useWeight > this.minWeight && this.useWeight <= this.maxWeight){
            return "정상 몸무게입니다.";
        } else if (this.useWeight < this.minWeight){
            return "살 좀 찌세요~";
        } else {
            return "살 좀 빼세요~"
        }
    }

}
let hwang = new CheckWeight("웹쓰",163,50);
let lee = new CheckWeight("웹와이",200,110);

document.write(hwang.getInfo());
document.write(hwang.getResult());

6. 함수가 실행되었습니다.

function func6(num1,str1,str2){
        this.youNum = num1;
        this.youStr1 = str1;
        this.youStr2 = str2;
        this.result = function(){
            //let str = this.youNum + this.youStr1 + this.youStr2;
            str += this.youNum + ".";
            str += this.youStr1 + "가";
            str += this.youStr2 + "실행되었습니다.";
            return str;

        }
}
let java = new func6(6,"함수","실행");
document.write(java.result());

Last updated

Was this helpful?