JavaScript

[Functions] JS - apply() 메서드 - 함수호출방법3 (= 어플라이메서드)

apply() 메서드 사용하면 다른 객체에서 사용 가능한 메서드 작성 가능.


apply() 메서드

 

1.

apply() 메서드는 미리 정의된 JS 메서드임.

2.
call() 메서드와 유사하나, 독립변수 받을 때 차이 있음. 
call() 메서드는 개별적으로, apply() 메서드는 배열로 받음.

 


 

<p id="demo"></p>


<script>

var site = {

  plus: function() {

    return this.name + " " + this.host;

  }

}

var site1 = {

  name: "홈짱닷컴",

  host: "Homzzang.com"

}

var site2 = {

  name:"그누보드",

  host: "sir.kr"

}

var x = site.plus.apply(site1); //  site1에서 site의 plus 메서드 호출

var y = site.plus.apply(site2); //  site2에서 site의 plus 메서드 호출

document.getElementById("demo").innerHTML = x + "<br>" + y;   

</script>

 

 

결과보기

홈짱닷컴 Homzzang.com

그누보드 sir.kr

 

apply() vs. call() 차이점

 

call()

독립변수를 개별로 취함.

apply()
독립변수를 배열로 취함. (※ 독립변수 많을 때 편리.)

 

 

apply() 메서드에 독립변수 사용하기

 

<p id="demo"></p>


<script>

var site = {

  plus: function(openintro) {

    return this.name + " " + this.host + " (" + open + ") : " + intro;

  }

}

var site1 = {

  name: "홈짱닷컴",

  host: "Homzzang.com"

}

var site2 = {

  name: "그누보드",

  host: "sir.kr"

}

var x = site.plus.apply(site1, ["2012", "홈페이지 제작강의"]); 

var y = site.plus.apply(site2, ["2001", "그누보드5 영카트"]);

document.getElementById("demo").innerHTML = x + "<br>" + y; 

</script>

 

결과보기

홈짱닷컴 Homzzang.com (2012) : 홈페이지 제작강의
그누보드 sir.kr (2001) : 그누보드5 영카트

 

max() 메서드를 배열에 적용하기

 

1.
JS 배열이 아닌 경우 최대값 구하기 : 
Math.max() 메서드 이용


<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.max(3,4,5)

</script>

 

결과보기


 

2.

JS 배열 경우엔 max() 메서드 존재 안 하므로, 아래 방식으로 최대값 산출 가능.


Math.max.apply(null, array)

 

null

임의의 값.


array
배열. (예) [a, b, c]

 






※ 아래 예제들은 모두 동일 결과 출력.



예제1

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.max.apply(null, [3,4,5]); // 5

</script>

 

결과보기


예제2

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.max.apply(Math, [3,4,5]); // 5

</script>

 

결과보기


예제3

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.max.apply(" ", [3,4,5])

</script

 

결과보기


예제4

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.max.apply(0, [3,4,5]); 

</script>

 

결과보기

 

 

apply() 메서드의 첫번째 독립변수가 객체 아닌 경우

 

JS 엄격모드 경우, 첫번째 객체는 호출된 함수의 소유자 (객체)가 됨. 

JS 일반모드 경우, 첫번째 객체는 전역객체가 됨. 

 



분류 제목
DOM_Style JS - flexShrink 속성 - 동일컨테이너 안 나머지 플렉스아이템에 비해 얼마나 줄어들지 지정 (= …
DOM_Style JS - flexWrap -
DOM_Style JS - cssFloat 속성 - ★ - 요소나란히 (= 요소한줄로 = 요소가로로 = 요소옆으로정렬 = 플로…
DOM_Style JS - font 속성 ★ - 글자스타일종합 (= font속성 = 글씨체 = 서체 = 글꼴 = 폰트속성, 상…
DOM_Style JS - fontFamily 속성 ★ - 폰트 종류 설정/반환. (= 글꼴 종류 = fontFamily속성 …
DOM_Style JS - fontSize 속성 - 글자크기 (= 폰트크기 = fontSize속성 = 폰트사이즈 속성)
DOM_Style JS - fontStyle 속성 - 글자모양 (= font-style속성 = 글씨체모양 = 글씨모양 = 글자…
DOM_Style JS - fontVariant 속성 - (소문자/보통) 크기의 대문자로 설정/반환.
DOM_Style JS - fontWeight 속성 - 글자굵기 (= font-weight속성 = 폰트굵기 = 글씨체굵기 =글…
DOM_Style JS - fontSizeAdjust -
DOM_Style JS - fontStretch -
DOM_Style JS - hangingPunctuation -
DOM_Style JS - height 속성 - 요소 높이 설정/반환 (= height속성 = 하이트속성) ※ 요소 세로길이 …
DOM_Style JS - hyphens -
DOM_Style JS - icon -
62/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인