Function.prototype.apply()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2015๋ 7์โฉ.
apply()
๋ฉ์๋๋ ์ฃผ์ด์ง this
๊ฐ๊ณผ ๋ฐฐ์ด (๋๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด) ๋ก ์ ๊ณต๋๋ arguments
๋ก ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
์ฐธ๊ณ :
์ด ํจ์์ ๊ตฌ๋ฌธ์ ๊ฑฐ์ call()
๊ตฌ๋ฌธ๊ณผ ์ ์ฌํฉ๋๋ค. ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์ ์ call()
์ ํจ์์ ์ ๋ฌ๋ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋๋ฐ ๋นํด, apply()
๋ ์ธ์๋ค์ ๋จ์ผ ๋ฐฐ์ด์ ๋ฐ๋๋ค๋ ์ ์
๋๋ค.
์๋ํด ๋ณด๊ธฐ
const numbers = [5, 6, 2, 3, 7];
const max = Math.max.apply(null, numbers);
console.log(max);
// Expected output: 7
const min = Math.min.apply(null, numbers);
console.log(min);
// Expected output: 2
๊ตฌ๋ฌธ
func.apply(thisArg, [argsArray]);
๋งค๊ฐ๋ณ์
thisArg
-
์ต์ .
func
๋ฅผ ํธ์ถํ๋๋ฐ ์ ๊ณต๋this
์ ๊ฐ.this
๋ ๋ฉ์๋์ ์ํด ์ค์ ๋ก ๋ณด์ฌ์ง๋ ๊ฐ์ด ์๋ ์ ์์์ ์ ์ํฉ๋๋ค. ๋ฉ์๋๊ฐ non-strict mode ์ฝ๋์ ํจ์์ผ ๊ฒฝ์ฐ,null
๊ณผundefined
๊ฐ ์ ์ญ ๊ฐ์ฒด๋ก ๋์ฒด๋๋ฉฐ, ๊ธฐ๋ณธ ๊ฐ์ ์ ํ๋ฉ๋๋ค. argsArray
-
์ต์ .
func
์ด ํธ์ถ๋์ด์ผ ํ๋ ์ธ์๋ฅผ ์ง์ ํ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด, ํจ์์ ์ ๊ณต๋ ์ธ์๊ฐ ์์ ๊ฒฝ์ฐnull
๋๋undefined
. ECMAScript 5 ์ ์์์ผ๋ก ์ด๋ฌํ ์ธ์๋ค์ ๋ฐฐ์ด ๋์ ์ ๋ค๋ฆญ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค. ์๋์ ๋ธ๋ผ์ฐ์ ํธํ์ฑ ์ ๋ณด๋ฅผ ๋ณด์ธ์.
๋ฐํ๊ฐ
์ง์ ํ this
๊ฐ๊ณผ ์ธ์๋ค๋ก ํธ์ถํ ํจ์์ ๊ฒฐ๊ณผ.
์ค๋ช
์ด๋ฏธ ์กด์ฌํ๋ ํจ์๋ฅผ ํธ์ถํ ๋ ๋ค๋ฅธ this
๊ฐ์ฒด๋ฅผ ํ ๋นํ ์ ์์ต๋๋ค. this
๋ ํ์ฌ ๊ฐ์ฒด, ํธ์ถํ๋ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํฉ๋๋ค. apply
๋ฅผ ์ฌ์ฉํด, ์๋ก์ด ๊ฐ์ฒด๋ง๋ค ๋ฉ์๋๋ฅผ ์ฌ์์ฑํ ํ์์์ด ํ ๋ฒ๋ง ์์ฑํด ๋ค๋ฅธ ๊ฐ์ฒด์ ์์์ํฌ ์ ์์ต๋๋ค.
apply
๋ ์ง์๋๋ ์ธ์์ ํ์
๋ง ์ ์ธํ๋ฉด call()
๊ณผ ๋งค์ฐ ์ ์ฌํฉ๋๋ค. ์ธ์(ํ๋ผ๋ฏธํฐ)์ ๋ฆฌ์คํธ ๋์ ์ธ์๋ค์ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ํ apply
๋ฅผ ์ฌ์ฉํด, ๋ฐฐ์ด ๋ฆฌํฐ๋ด์ด๋ (์, func.apply(this, ['eat', 'bananas']), Array
๊ฐ์ฒด (์, func.apply(this, new Array('eat', 'bananas'))) ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
argsArray
ํ๋ผ๋ฏธํฐ๋ฅผ ์ํ arguments
๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค. arguments
๋ ํจ์์ ์ง์ญ ๋ณ์์
๋๋ค. ์ด๋ ํธ์ถ๋ ๊ฐ์ฒด์ ์ง์ ๋์ง ์์ ๋ชจ๋ ์ธ์์ ๋ํด ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์, apply
๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋ ํธ์ถ๋ ๊ฐ์ฒด์ ์ธ์๋ฅผ ์ ํ์๊ฐ ์์ต๋๋ค. arguments
๋ฅผ ์ฌ์ฉํด ๋ชจ๋ ์ธ์๋ค์ ํธ์ถ๋ ๊ฐ์ฒด๋ก ์ ๋ฌํ ์ ์์ต๋๋ค. ๊ทธ๋ผ ํธ์ถ๋ ๊ฐ์ฒด๋ ๊ทธ ์ธ์๋ค์ ์ฒ๋ฆฌํ ์ ์๊ฒ ๋ฉ๋๋ค.
ECMAScript 5๋ฒ ์งธ ํ์ ์์์ผ๋ก, ๋ชจ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด ํ์
์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ค์ ๋ก ์ด๋ ํ๋กํผํฐ length
์ ๋ฒ์ (0..length-1)
๋ด์ ์ ์ ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด, ์ด์ NodeList
๋๋ { 'length': 2, '0': 'eat', '1': 'bananas' }
์ ๊ฐ์ ์ปค์คํ
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : Chrome 14์ Internet Explorer 9๋ฅผ ํฌํจํ ๋๋ถ๋ถ์ ๋ธ๋ผ์ฐ์ ๋ ์์ง ์ ์ฌ๋ฐฐ์ด๊ฐ์ฒด๋ฅผ apply์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ์ค๋ฅ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
์์
๋ฐฐ์ด์ ๋ฐฐ์ด์ ๋ถ์ด๊ธฐ ์ํด apply
์ฌ์ฉํ๊ธฐ
push
๋ฅผ ์ฌ์ฉํ์ฌ ์์๋ฅผ ๋ฐฐ์ด์ ์ถ๊ฐ ํ ์ ์์ต๋๋ค. push
๋ ๊ฐ๋ณ ์ธ์๋ฅผ ํ์ฉํ๊ธฐ ๋๋ฌธ์ ์ฌ๋ฌ ์์๋ฅผ ๋์์ ์ถ๊ฐ ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ push
์ ๋ฐฐ์ด์ ์ ๋ฌํ๋ฉด ์์๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ์ถ๊ฐํ๋ ๋์ ์ค์ ๋ก ํด๋น ๋ฐฐ์ด์ ๋จ์ผ ์์๋ก ์ถ๊ฐํ๋ฏ๋ก ๊ฒฐ๊ตญ ๋ฐฐ์ด ๋ด๋ถ์ ๋ฐฐ์ด๋ก ๋๋ฉ๋๋ค. ๊ทธ๊ฒ์ด ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒ์ด ์๋๋ผ๋ฉด? ์ด ๊ฒฝ์ฐ concat
์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋์์ ํ์ง๋ง ์ค์ ๋ก๋ ๊ธฐ์กด ๋ฐฐ์ด์ ์ถ๊ฐ๋์ง ์๊ณ ์ ๋ฐฐ์ด์ ๋ง๋ค์ด ๋ฐํ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ ๊ธฐ์กด ๋ฐฐ์ด์ ์ถ๊ฐํ๊ณ ์ถ์์ต๋๋ค ... ๊ทธ๋ผ ์ด์ ์ด์ฉ์ฃ ? ๋ฃจํ๋ฅผ ์์ฑ ํ ๊น์? ๋ถ๋ช
ํ ์๋์ฃ ?
๋ฐฉ๋ฒ์ apply
!
var array = ["a", "b"];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); // ["a", "b", 0, 1, 2]
apply
์ ๋ด์ฅํจ์ ์ฌ์ฉ
apply
๋ฅผ ๋ณด๋ค ํจ๊ณผ์ ์ผ๋ก ์ด์ฉํ๋ฉด ์ผ๋ถ ๋ด์ฅ ํจ์๋ ์ด๋ค ์์
์ ๋ํด์๋ ๋ฐฐ์ด๊ณผ ๋ฃจํ์์ด ์ฝ๊ฒ ์ฒ๋ฆฌ๋ฉ๋๋ค. ๋ค์ ์์ ์์๋ ๋ฐฐ์ด์์ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ๊ตฌํ๊ธฐ ์ํด Math.max
/Math.min
ํจ์๋ฅผ ์ด์ฉํ๊ณ ์์ต๋๋ค.
// min/max number in an array
var numbers = [5, 6, 2, 3, 7];
// using Math.min/Math.max apply
var max = Math.max.apply(null, numbers);
// ์ด๋ Math.max(numbers[0], ...) ๋๋ Math.max(5, 6, ...)
// ์ ๊ฑฐ์ ๊ฐ์
var min = Math.min.apply(null, numbers);
// vs. simple loop based algorithm
((max = -Infinity), (min = +Infinity));
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
ํ์ง๋ง ์ด๋ฌํ ๋ฐฉ์์ผ๋ก apply
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฃผ์ํด์ผ ํฉ๋๋ค. JavaScript ์์ง์ ์ธ์ ๊ธธ์ด ์ ํ์ ์ด๊ณผํ๋ ์ํ์ฑ์ ๋ํด ์ดํดํ ํ์๊ฐ ์์ต๋๋ค. ํจ์์ ๋๋ฌด ๋ง์(๋๋ต ๋ช ๋ง๊ฐ ์ด์) ์ธ์๋ฅผ ์ค ๋์ ์ํฉ์ ์์ง๋ง๋ค ๋ค๋ฅธ๋ฐ(์๋ฅผ ๋ค์ด JavaScriptCore์ ๊ฒฝ์ฐ ์ธ์์ ๊ฐ์ ์ ํ์ 65536), ์ํ์ด ํน๋ณํ ์ ํด์ ธ ์์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด๋ค ์์ง์ ์์ธ๋ฅผ ๋์ง๋๋ค. ๋ ์ฌํ ๊ฒฝ์ฐ๋ ์ค์ ํจ์์ ์ธ์๋ฅผ ์ ๋ฌํ์์๋ ๋ถ๊ตฌํ๊ณ ์ฐธ์กฐํ ์ ์๋ ์ธ์์ ์๋ฅผ ์ ํํ๊ณ ์๋ ๊ฒฝ์ฐ๋ ์์ต๋๋ค(์ด๋ฌํ ์์ง์ ๋ํด ๋ ์์ธํ ์ค๋ช
ํ๋ฉด, ๊ทธ ์์ง์ด arguments์ ์ํ์ 4๊ฐ๋ก ํ๋ค๊ณ ํ๋ฉด[์ค์ ์ํ์ ๋ฌผ๋ก ๋ ์์ผ ๊ฒ์
๋๋ค], ์ ์์ ์ฝ๋์ ์ ์ฒด ๋ฐฐ์ด์ด ์๋๋ผ 5, 6, 2, 3
๋ง apply
์ ์ ๋ฌ๋์ด ์จ ๊ฒ์ฒ๋ผ ์๋ํฉ๋๋ค).
๋ง์ฝ ์ฌ์ฉํ๋ ๋ฐฐ์ด ๋ณ์์ ์๊ฐ ์๋ง์ ์ด๊ณผํ๋ ๊ฒฝ์ฐ์๋ ๋ณตํฉ์ ์ธ ์ ๋ต์ ๊ฐ๊ตฌํด์ผํ ๊ฒ์ ๋๋ค:ํ ๋ฒ์ ์ ๋ฌํ ๋ฐฐ์ด์ ๋ถํ ํ์ฌ ์ฌ์ฉํ๊ธฐ:
function minOfArray(arr) {
var min = Infinity;
var QUANTUM = 32768;
for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
var submin = Math.min.apply(null, arr.slice(i, Math.min(i + QUANTUM, len)));
min = Math.min(submin, min);
}
return min;
}
var min = minOfArray([5, 6, 2, 3, 7]);
์์ฑ์ ์ฒด์ด๋์ ์ํ apply
์ฌ์ฉ
Java ์ ์ ์ฌํ๊ฒ, ๊ฐ์ฒด๋ฅผ ์ํ constructors ์ฒด์ด๋์ ์ํด apply
๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค์ ์์ ์์ ์ธ์ ๋ฆฌ์คํธ ๋์ ์์ฑ์๋ก ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ construct
๋ผ๋ ์ ์ญ Function
๋ฉ์๋๋ฅผ ์์ฑํ ๊ฒ์
๋๋ค.
Function.prototype.construct = function (aArgs) {
var oNew = Object.create(this.prototype);
this.apply(oNew, aArgs);
return oNew;
};
์ฐธ๊ณ : ์๋ฆผ: ์์์ ์ฌ์ฉ๋
Object.create()
๋ฉ์๋๋ ์๋์ ์ผ๋ก ์๋ก์ด ๊ฒ์ ๋๋ค. ๋์์ผ๋ก, ๋ค์ ์ ๊ทผ๋ฒ ์ค ํ๋๋ฅผ ๊ณ ๋ คํ์ธ์.
Object.prototype.__proto__
์ฌ์ฉ:
Function.prototype.construct = function (aArgs) {
var oNew = {};
oNew.__proto__ = this.prototype;
this.apply(oNew, aArgs);
return oNew;
};
ํด๋ก์ ธ ์ฌ์ฉ:
Function.prototype.construct = function(aArgs) {
var fConstructor = this, fNewConstr = function() {
fConstructor.apply(this, aArgs);
};
fNewConstr.prototype = fConstructor.prototype;
return new fNewConstr();
};
Function
์์ฑ์ ์ฌ์ฉ
Function.prototype.construct = function (aArgs) {
var fNewConstr = new Function("");
fNewConstr.prototype = this.prototype;
var oNew = new fNewConstr();
this.apply(oNew, aArgs);
return oNew;
};
์ฌ์ฉ ์์
function MyConstructor() {
for (var nProp = 0; nProp < arguments.length; nProp++) {
this["property" + nProp] = arguments[nProp];
}
}
var myArray = [4, "Hello world!", false];
var myInstance = MyConstructor.construct(myArray);
console.log(myInstance.property1); // logs 'Hello world!'
console.log(myInstance instanceof MyConstructor); // logs 'true'
console.log(myInstance.constructor); // logs 'MyConstructor'
์๋ฆผ: ๋ค์ดํฐ๋ธ๊ฐ ์๋ Function.construct
๋ฉ์๋๋ Date
์ ๊ฐ์ ์ผ๋ถ ๋ค์ดํฐ๋ธ ์์ฑ์์ ๋์ํ์ง ์์ ๊ฒ์
๋๋ค. ๊ทธ๋ฐ ๊ฒฝ์ฐ, Function.prototype.bind
๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ค์๊ณผ ๊ฐ์ ๋ฐฐ์ด์ด ์๋ค๊ณ ํ ๋, Date
์์ฑ์: [2012, 11, 4]
์ ํจ๊ป ์ฌ์ฉ๋๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํด์ผ ํฉ๋๋ค: new (Function.prototype.bind.apply(Date, [null].concat([2012, 11, 4])))()
. ์ด๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ด ์๋๋ฉฐ, ์ด๋ค ์์ฉ ํ๊ฒฝ์์๋ ์ฌ์ฉ๋์ง ์์ ์ ์์ต๋๋ค.
๋ช ์ธ
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-function.prototype.apply |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ