본문 바로가기
Javascript

함수 02

by 🐬마뇽뇽 마뇽🦄 2022. 8. 22.
728x90

함수 02

함수에 변수를 이용해 여러가지를 출력하거나, 객체 안에 변수를 넣는 등 프로토 타입이나 객체 리터럴 함수를 정리해보았습니다.

01. 함수와 매개변수를 이용한 형태

func에 변수를 저장 하였을 때, 다시 매개변수를 통해 값을 저장해 출력할 수 있었습니다.

function func(str1, str2){
    document.write(str1 + "와" + str2 + "는 귀엽다." )
}

func("강아지","고양이");
func("햄스터", "토끼")


02. 함수와 변수를 이용한 형태

func에 변수를 저장 하였을 때, 다시 const에 변수를 출력할 수 있었습니다.

function func(str1, str2){
    document.write(str1 + "는" + str2 + "가 귀엽다.")
}

const youNum1 = "강아지"
const youNum2 = "고양이"
const youStr1 = "혓바닥";
const youStr2 = "발바닥";

func(youNum1,youStr1);
func(youNum2,youStr2);


03. 함수와 배열, 객체를 이용한 형태

function func(str1, str2){
    document.write(str1 + "는" + str2 + "가 귀엽다.")
}

cosnt info = {
    {
        mai : 강아지,
        num : 혓바닥
    },
    {
         mai :고양이,
        num : 발바닥
    }
}

func(info[0].mai,info[0].num);
func(info[1].mai,info[1].num);


04. 객체 안에 변수와 함수를 이용한 형태

cosnt info = {
    mai1 : 강아지,
    num1: 혓바닥,
    mai2 :고양이,
    num2 : 발바닥

    result1 : function(){
         document.write(info.mai1 + "는" + info.num1 + "가 귀엽다." )
    },
    result2 : function(){
         document.write(info.mai2 + "는" + info.num2 + "가 귀엽다." )
    }
}

info.result1();
info.result2();


05. 객체 생성자 함수

function func(str, mai){

     this str = str
    this mai = mai

    this.result = function(){
         document.write(str + "는" + mai + "가 귀엽다.");
    }
}

const info1 = new func("강아지","혓바닥");
const info2 = new func("고양이","발바닥");

info1.result();
info2.result();


06. 프로토 타입 함수

function func(str, mai){

    this str = str
    this mai = mai

    func.prototype.result = function(){
         document.write(str + "는" + mai + "가 귀엽다.");
    }
}

const info1 = new func("강아지","혓바닥");
const info2 = new func("고양이","발바닥");

info1.result();
info2.result();


07. 객체 리터럴 함수

function func(str, mai){

    this str = str
    this mai = mai
}
func.prototype = {
    result1 : function(){
        document.write(str + "는" + mai + "가 귀엽다.");
    },
    result2 : function(){
        document.write(str + "는" + mai + "가 귀엽다.");
    },
}

const info1 = new func("강아지","혓바닥");
const info2 = new func("고양이","발바닥");

info1.result();
info2.result();


728x90

'Javascript' 카테고리의 다른 글

match()  (6) 2022.08.22
charAt()  (6) 2022.08.22
includes()  (4) 2022.08.17
padStart() / padEnd()  (4) 2022.08.17
repeat()  (4) 2022.08.17

댓글


고양이 고양이
고양이 고양이
고양이 고양이