Function Declarations vs Function Expressions - Lee-hyuna/33-js-concepts-kr GitHub Wiki

ํ•จ์ˆ˜ ์„ ์–ธ์‹๊ณผ ํ•จ์ˆ˜ ํ‘œํ˜„์‹

์›๋ฌธ : https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

์งง์€ ํ€ด์ฆˆ๋ฅผ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ผ€์ด์Šค์—์„œ alert์—๋Š” ๋ฌด์—‡์ด ์žˆ์„๊นŒ์š”?

Question 1:

function foo(){
    function bar() {
        return 3;
    }
    return bar();
    function bar() {
        return 8;
    }
}
alert(foo());
function foo(){
    var bar = function() {
        return 3;
    };
    return bar();
    var bar = function() {
        return 8;
    };
}
alert(foo());
alert(foo());
function foo(){
    var bar = function() {
        return 3;
    };
    return bar();
    var bar = function() {
        return 8;
    };
}
function foo(){
    return bar();
    var bar = function() {
        return 3;
    };
    var bar = function() {
        return 8;
    };
}
alert(foo());

๋งŒ์•ฝ 8,3,3์„ ๋Œ€๋‹ตํ• ์ˆ˜ ์—†๊ฑฐ๋‚˜ [Type Error: bar is not a function]์ด ๋‚˜์˜จ๋‹ค๋ฉด ์ด ๊ธ€์„ ์ฝ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์‹ค์ œ๋ก  ์–ด๋Š ๋ˆ„๊ตฌ๋“  ์ฝ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค.๐Ÿ˜‰)

ํ•จ์ˆ˜ ์„ ์–ธ์‹์ด ๋ฌด์—‡์ธ๊ฐ€์š”?

ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ ๋ณ€์ˆ˜ ํ• ๋‹น์„ ์š”๊ตฌํ•˜์ง€ ์•Š๊ณ  ๋ช…๋ช…๋œ ํ•จ์ˆ˜ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ ์–ธ์€ ๋…๋ฆฝ ์‹คํ–‰ํ˜• ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐœ์ƒํ•˜๋ฉฐ ๋น„-ํ•จ์ˆ˜ ๋ธ”๋ก ๋‚ด์— ์ค‘์ฒฉ๋ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ์–ธ์˜ ํ˜•์ œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ์–ธ์ด โ€œvarโ€๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ โ€œfunctionโ€์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

function bar() {
    return 3;
}

ECMA 5 (13.0)์˜ javascript function Identifier ( FormalParameterListopt ) { FunctionBody } ๋ฌธ๋ฒ•์œผ๋กœ ์ •์˜ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„์€ scope๋‚ด์—์„œ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, scope๋Š” ๋ถ€๋ชจ ์ž…๋‹ˆ๋‹ค. (๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋„๋‹ฌ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ข‹์Šต๋‹ˆ๋‹ค.)

function bar() {
    return 3;
}
 
bar() //3
bar  //function

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์ด ๋ฌด์—‡์ธ๊ฐ€์š”?

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ๋” ๋งŽ์€ ํ‘œํ˜„์˜ ๋ฌธ๋ฒ•(์ผ๋ฐ˜์ ์œผ๋กœ ๋ณ€์ˆ˜์˜ ํ• ๋‹น)์˜ ์ผ๋ถ€๋กœ ์ •์˜ ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ํ†ตํ•ด ์ •์˜๋œ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ ์ต๋ช…์œผ๋กœ ์ง€์ •ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ โ€œfunctionโ€ ์œผ๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. (๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ ์ง์ ‘ ํ˜ธ์ถœ์˜ ์˜ˆ์ œ์—์„œ ๊ด„ํ˜ธ)

//anonymous function expression
var a = function() {
    return 3;
}
 
//named function expression
var a = function bar() {
    return 3;
}
 
//self invoking function expression
(function sayHello() {
    alert("hello!");
})();

ECMA 5 (13.0)์˜ function Identifieropt ( FormalParameterListopt ) { FunctionBody } ๋ฌธ๋ฒ•์œผ๋กœ ์ •์˜ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. โ€œfunctionโ€์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•์ด ํฌํ•จ๋˜์–ด์ง„ ์š”๊ตฌ์‚ฌํ•ญ์œผ๋กœ ์ธํ•ด ๋ถˆ์™„์ „ ํ•˜๋‹ค๊ณ  ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค. (though this feels incomplete since it omits the requirement that the containing syntax be an expression and not start with โ€œfunctionโ€>์˜์—ญ)

ํ•จ์ˆ˜ ์ด๋ฆ„(์กด์žฌ ํ•˜๋Š” ๊ฒฝ์šฐ)์€ ํ•ด๋‹น scope ๋ฐ–์—์„œ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. (ํ•จ์ˆ˜ ์„ ์–ธ์‹๊ณผ ๋Œ€์กฐ๊ฐ€ ๋œ๋‹ค.)

๊ทธ๋ž˜์„œ ํ•จ์ˆ˜๋ฌธ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?

๋•Œ๋กœ๋Š” ํ•จ์ˆ˜ ์„ ์–ธ์„ ์œ„ํ•œ ๊ฐ€๋ช…์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ kangax๊ฐ€ ์ง€์ ํ–ˆ๋“ฏ์ด mozilla์—์„œ ํ•จ์ˆ˜๋ฌธ์€ ํ•จ์ˆ˜ ์„ ์–ธ์˜ ํ™•์žฅ์œผ๋กœ ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์„ ํ•จ์ˆ˜๋ฌธ์„ ํ—ˆ์šฉ๋˜๋Š” ๋ถ€๋ถ„ ์–ด๋””์—์„œ๋‚˜ ํ•จ์ˆ˜ ์„ ์–ธ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (in mozilla a Function Statement is an extension of Function Declaration allowing the Function Declaration syntax to be used anywhere a statement is allowed.>์–ด๋–ป๊ฒŒ ์„ค๋ช…์„ ํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์„๊นŒ..) ์•„์ง ํ‘œ์ค€์ด ์•„๋‹ˆ๋ฏ€๋กœ production development์— ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ€ด์ฆˆ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ค„๊นŒ์š”?

์ข‹์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ 1์€ ํ•จ์ˆ˜ ์„ ์–ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ hoisted๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ž ์‹œ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”. Hoisting์ด ๋ฌด์—‡์ธ๊ฐ€์š”?

Ben Cherry์˜ ๋›ฐ์–ด๋‚œ ๊ธฐ์‚ฌ๋ฅผ ์ธ์šฉํ•ฉ๋‹ˆ๋‹ค. โ€œFunction declarations and function variables are always moved (โ€˜hoistedโ€™) to the top of their JavaScript scope by the JavaScript interpreterโ€

ํ•จ์ˆ˜ ์„ ์–ธ์ด ์ƒ๋‹จ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฉด ์ „์ฒด ํ•จ์ˆ˜ ๋ณธ๋ฌธ์ด ์ƒ๋‹จ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์งˆ๋ฌธ1์˜ ์ฝ”๋“œ๊ฐ€ ๋๋‚œ ํ›„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

//**Simulated processing sequence for Question 1**
function foo(){
    //define bar once
    function bar() {
        return 3;
    }
    //redefine it
    function bar() {
        return 8;
    }
    //return its invocation
    return bar(); //8
}
alert(foo());

ํ•˜์ง€๋งŒ...ํ•˜์ง€๋งŒ..์šฐ๋ฆฌ๋Š” ํ•ญ์ƒ ๋ฐ˜ํ™˜๋ฌธ์ด ๋„๋‹ฌ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์›Œ์™”์Šต๋‹ˆ๋‹ค.

Javascript์‹คํ–‰์—๋Š” Context(ECMA 5๊ฐ€ LexicalEnvironment, VariableEnvironment, ThisBinding์œผ๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ) ๋ฐ Process (์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœ๋  ๋ช…๋ น๋ฌธ ์„ธํŠธ)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ scope๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์„ ์–ธ์ด VariableEnvironment์— ๊ธฐ์—ฌํ•ฉ๋‹ˆ๋‹ค. Context์™€ Process๋Š” return๊ณผ ๊ฐ™์€ Statement์™€ ๊ตฌ๋ณ„๋˜๊ณ , process์˜ ๊ทœ์น™์— ๋Œ€ํ•œ ์ ์šฉ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์ด ํ˜ธ์ด์ŠคํŠธ ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๊นŒ?

๊ทธ๊ฒƒ์€ ํ‘œํ˜„์‹์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ํ€ด์ฆˆ 2๋ฒˆ์งธ์˜ ์ฒซ๋ฒˆ์งธ ํ‘œํ˜„์‹์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

var bar = function() {
    return 3;
};

์™ผ์ชฝ์˜ var bar์€ ๋ณ€์ˆ˜ ์„ ์–ธ ์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ์–ธ์€ ์ƒ๋‹จ์œผ๋กœ ์ด๋™์ด ๋˜์—ˆ์ง€๋งŒ ํ• ๋‹น ํ‘œํ˜„์‹์€ ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ bar๊ฐ€ ์ƒ๋‹จ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์ดˆ๊ธฐ์— var bar = undefined์„ ์„ค์ • ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ •์˜ ์ž์ฒด๋Š” ํ˜ธ์ด์ŠคํŠธ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (ECMA 5 12.2 ์—์„œ ์ดˆ๊ธฐํ™”๊ฐ€ ์ด๋ค„์ง„ ๋ณ€์ˆ˜์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๋•Œ๊ฐ€ ์•„๋‹ˆ๋ผ VariableStatement๊ฐ€ ์‹คํ–‰์ด ๋  ๋•Œ AssignmentExpressionr๊ฐ’์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.)

๋”ฐ๋ผ์„œ ํ€ด์ฆˆ 2์˜ ์ฝ”๋“œ๋Š” ๋ณด๋‹ค ์ง๊ด€์ ์ธ ์ˆœ์„œ๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.

//**Simulated processing sequence for Question 2**
function foo(){
    //a declaration for each function expression
    var bar = undefined;
    var bar = undefined;
    //first Function Expression is executed
    bar = function() {
        return 3;
    };
    // Function created by first Function Expression is invoked
    return bar();
    // second Function Expression unreachable
}
alert(foo()); //3

์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ง์ด ๋งž๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฑด ๊ทธ๋ ‡๊ณ  ์งˆ๋ฌธ 3์— ๋Œ€ํ•ด์„œ ํ‹€๋ ธ๋Š”๋ฐ Firebug์—์„œ error๊ฐ€ ๋ฐœ์ƒ ํ–ˆ์Šต๋‹ˆ๋‹ค.

HTML ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  Firefox์—์„œ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”. ๋˜๋Š” IE8, Chrome ๋˜๋Š” Safari ์ฝ˜์†”์—์„œ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”. ๋ถ„๋ช…ํžˆ Firebug ์ฝ˜์†”์€ โ€œglobalโ€ scope์—์„œ ์‹คํ–‰๋  ๋•Œ ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…์„ ์—ฐ์Šตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (์‹ค์ œ๋กœ โ€œglobalโ€์ด ์•„๋‹ˆ์ง€๋งŒ ํŠน๋ณ„ํ•œ โ€œFirebugโ€์˜ scope - โ€œFirebugโ€์˜ ์ฝ˜์†”์—์„œ๋Š” โ€œthis == windowโ€์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.)

ํ€ด์ฆˆ 3์€ ํ€ด์ฆˆ 1๊ณผ ์œ ์‚ฌํ•œ ๋…ผ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” hoisted๋œ foo ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

์ด์ œ ํ€ด์ฆˆ 4๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํ•จ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๊ฑฐ์˜. ํ˜ธ์ด์ŠคํŒ…์ด ๋ชจ๋‘ ์—†์œผ๋ฉด TypeError๋Š” โ€œbar not definedโ€๊ฐ€ ๋˜๊ณ  โ€œbar not a functionโ€์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…๊ฐ€ ์—†์ง€๋งŒ ๊ฐ€๋ณ€ ํ˜ธ์ด์ŠคํŒ…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ bar๋Š” ์•ž์— ์„ ์–ธ๋˜์—ˆ์ง€๋งŒ ๊ฐ’์€ ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฒƒ์€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (Everything else runs to order. ์—ฌ๊ธฐ์—์„œ order ํ•ด์„์€...?)

//**Simulated processing sequence for Question 4**
function foo(){
    //a declaration for each function expression
    var bar = undefined;
    var bar = undefined;
    return bar(); //TypeError: "bar not defined"
    //neither Function Expression is reached
}
alert(foo());

์–ด๋–ค ๊ฒƒ์„ ์ฃผ์˜ ํ•ด์•ผํ•ฉ๋‹ˆ๊นŒ?

ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋ธ”๋ก(ex:if) ๋‚ด์—์„œ ๊ณต์‹์ ์œผ๋กœ ๊ธˆ์ง€๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋ฅผ ํ—ˆ์šฉํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ•ด์„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Firefox 3.6์˜ ๋‹ค์Œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์€ ํ•จ์ˆ˜ ์„ ์–ธ์„ ํ•จ์ˆ˜ ๊ตฌ๋ฌธ์œผ๋กœ ํ•ด์„ํ•˜๋ฏ€๋กœ x๋Š” ์ •์˜๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ IE8, Chrome 5 ๋ฐ Safari 5์—์„œ๋Š” ํ•จ์ˆ˜ x๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.(ํ‘œ์ค€ ํ•จ์ˆ˜ ์„ ์–ธ์‹์—์„œ ์˜ˆ์ƒ ํ•œ๋Œ€๋กœ)

function foo() {
    if(false) {
        function x() {};
    }
    return x;
}
alert(foo());

ํ•จ์ˆ˜ ์„ ์–ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜ผ๋™์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์ง€๋งŒ ์ด์ ์€ ์žˆ์Šต๋‹ˆ๊นŒ?

ํ•จ์ˆ˜ ์„ ์–ธ์ด ์šฉ์„œ๊ฐ€ ๋œ๋‹ค๊ณ  ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ์–ธ๋˜๊ธฐ ์ „์— ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๋ฉด ํ˜ธ์ด์ŠคํŒ…์ด ๋ช…๋ น์„ ์ˆ˜์ •ํ•œ ํ›„์— ๋ฌธ์ œ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ์šฉ์„œ๋Š” ๋‹จ๋‹จํ•œ ์ฝ”๋”ฉ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š์œผ๋ฉฐ ์žฅ๊ธฐ์ ์œผ๋กœ ์•„๋งˆ ๊ทธ๊ฒƒ์„ ๋ง‰๊ธฐ๋ณด๋‹ค๋Š” ๋†€๋ผ์›€์„ ์กฐ์žฅํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ด์œ ์— ๋”ฐ๋ผ ํŠน์ • ์ˆœ์„œ๋กœ ๋ช…๋ น๋ฌธ์„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ์„ ํ˜ธํ•˜๋Š” ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

์–ด๋–ป๊ฒŒ ์•„์…จ๋‚˜์š”?

a) ํ•จ์ˆ˜ ์„ ์–ธ์€ Java ์Šคํƒ€์ผ ๋ฉ”์„œ๋“œ ์„ ์–ธ์„ ๋ชจ๋ฐฉํ•˜๋ ค๋Š” ์˜๋„์˜€์ง€๋งŒ Java ๋ฉ”์„œ๋“œ๋Š” ๋งค์šฐ ๋‹ค๋ฅธ ์กด์žฌ๋ผ๊ณ  ๋Š๋‚๋‹ˆ๋‹ค. Javascript ํ•จ์ˆ˜์—์„œ๋Š” ๊ฐ’์ด ์žˆ๋Š”ํ•œ ์‚ด์•„์žˆ๋Š” ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค. Java ๋ฉ”์†Œ๋“œ๋Š” ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์Šค๋‹ˆํŽซ์€ ๋ชจ๋‘ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์ง€๋งŒ ํ•จ์ˆ˜ ํ‘œํ˜„์‹๋งŒ์ด ์šฐ๋ฆฌ๊ฐ€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Œ์„ ์•”์‹œํ•ฉ๋‹ˆ๋‹ค.

//Function Declaration
function add(a,b) {return a + b};
//Function Expression
var add = function(a,b) {return a + b};

b) ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ๋ณด๋‹ค ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ "๋ช…๋ น๋ฌธ"์œผ๋กœ๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ณ€์ˆ˜๊ฐ€ ๋ถ€๋ชจ ๋ณ€์ˆ˜์ธ ๊ฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ฟ ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํ•จ์ˆ˜ ํ‘œํ˜„์‹(์ •์˜์— ์˜ํ•œ)์€ ๋” ํฐ ๊ตฌ์กฐ์˜ ์ผ๋ถ€ ์ž…๋‹ˆ๋‹ค. ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ํ”„๋กœํ†  ํƒ€์ž…์— ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ์†์„ฑ์œผ๋กœ ํ• ๋‹นํ•˜๋ ค๋ฉด ํ•จ์ˆ˜ํ‘œํ˜„์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. curry ๋˜๋Š” compose์™€ ๊ฐ™์€ ๊ณ ์ฐจ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ‘œํ˜„์‹ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

//Function Expression
var sayHello = alert.curry("hello!");

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์— ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์— ์˜ํ•ด ์ƒ์„ฑ๋œ ํ•จ์ˆ˜๋Š” ์ด๋ฆ„์ด ์ง€์ •๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ์ต๋ช…์ด๋ฉฐ, ์˜ค๋Š˜๋‚ ์€ ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ฐธ์กฐ ์ผ๋ฟ์ž…๋‹ˆ๋‹ค.

var today = function() {return new Date()}

์ด๊ฒŒ ์ •๋ง ์ค‘์š”ํ• ๊นŒ์š”? Nick Fitzgerald๊ฐ€ ์ต๋ช… ํ•จ์ˆ˜๋กœ ๋””๋ฒ„๊น…์„ ํ•˜๋Š” ๊ฒƒ์€ ์ขŒ์ ˆ๊ฐ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ง€์ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ง์ž…๋‹ˆ๋‹ค. ๊ทธ๋Š” ๋ช…๋ช…๋œ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค.

var today = function today() {return new Date()}

๊ทธ๋Ÿฌ๋‚˜ Asen Bozhilov๊ฐ€ ์ง€์ ํ•œ ๋Œ€๋กœ(๊ทธ๋ฆฌ๊ณ  Kangax์˜ ๋ฌธ์„œ) NFE๋Š” IE 9 ํ•˜์œ„ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก ์€?

์ž˜๋ชป ๋†“์—ฌ์ง„ ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ ์˜คํ•ด์˜ ์†Œ์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฉด์ˆ˜์— ํ• ๋‹น๋œ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•จ์ˆ˜ ์„ ์–ธ์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํ•จ์ˆ˜ ์„ ์–ธ์‹์ด ์†ํ•œ ๋ฒ”์œ„๋ฅผ ๋งจ ์œ„์— ๋†“์œผ๋ฉด ํ˜ผ๋ˆ์„ ์ตœ์†Œํ™” ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” if ์„ ์–ธ๋ฌธ์— ํ•จ์ˆ˜ ์„ ์–ธ์‹์„ ๋‘์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๋ชจ๋“  ๊ฒƒ์„ ๋งํ•˜๋ฉด ํ•จ์ˆ˜ ์„ ์–ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํƒ€๋‹นํ•œ ์ƒํ™ฉ์—์„œ ์Šค์Šค๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ดœ์ฐฎ์•„. ๊ทœ์น™์— ์—„๊ฒฉํ•œ ์ค€์ˆ˜๋Š” ์œ„ํ—˜ํ•˜๋ฉฐ ๋•Œ๋กœ๋Š” ์‚๋šค์–ด์ง„ ์ฝ”๋“œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ›จ์”ฌ ๋” ์ค‘์š”ํ•œ ๊ฒƒ์€ ์Šค์Šค๋กœ ์ž์‹ ์˜ ์ •๋ณด์— ์ž…๊ฐํ•œ ๊ฒฐ์ •์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ๋…์„ ์ดํ•ดํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ธ€์ด ๋„์›€์ด ๋˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ