Baseline Widely available
í¨ì ì ì¸(function declaration)ì ì§ì ë 매ê°ë³ì(parameter)를 ê°ë í¨ì를 ì ìí©ëë¤.
Function
ìì±ìë í¨ì ííì(function expression)
ì ì¬ì©í´ì ì ìí ìë ììµëë¤.
function calcRectArea(width, height) {
return width * height;
}
console.log(calcRectArea(5, 6));
// Expected output: 30
구문
function name([param[, param,[..., param]]]) { [statements] }
name
í¨ì ì´ë¦.
param
í¨ìë¡ ì ë¬ëë ì¸ì(argument)ì ì´ë¦. ì¸ìì ìµë ê°ìë ìì§ë§ë¤ ë¤ë¦.
statements
í¨ìì 몸íµ(body)ì 구ì±íë 문(statement).
í¨ì ì ì¸ì¼ë¡ ìì±ë í¨ìë Function
ê°ì²´ë¡, Function
ê°ì²´ì 모ë ìì±(property), ë©ìë ë° íì í¹ì±(behavior)ì ê°ìµëë¤. í¨ìì ê´í ë ìì¸í ì ë³´ë Function
참조íì기 ë°ëëë¤.
í¨ìë ëí ííì(í¨ì ííì
참조)ì ì¬ì©íì¬ ìì±ë ì ììµëë¤.
기본ì ì¼ë¡ í¨ìë undefined
를 ë°íí©ëë¤. ë¤ë¥¸ ê°ì ë°íí기 ìí´ìë, í¨ìë ë°íê°ì ì§ì íë return
ë¬¸ì´ ìì´ì¼ í©ëë¤.
í¨ìë ì¡°ê±´ë¶ë¡ ì ì¸ë ì ììµëë¤. ì¦, function 문ì if
문 ìì ë¤ì´ê° ì ììµëë¤. íì§ë§, 구íì ë°ë¼ ê²°ê³¼ì ì¼ê´ì±ì´ ìì¼ë¯ë¡ ì´ í¨í´ì ì¤ì ì½ëììë ì¬ì©í´ì ìë©ëë¤. ì¡°ê±´ë¶ë¡ í¨ì를 ìì±íê³ ì íë¤ë©´, í¨ì ííì(function expression)ì ëì ì¬ì©íì¸ì.
var hoisted = "foo" in this;
console.log(
`'foo' name ${
hoisted ? "is" : "is not"
} hoisted. typeof foo is ${typeof foo}`,
);
if (false) {
function foo() {
return 1;
}
}
// In Chrome:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Firefox:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Edge:
// 'foo' name is not hoisted. typeof foo is undefined
//
// In Safari:
// 'foo' name is hoisted. typeof foo is function
ê²°ê³¼ë ì°¸ì¼ë¡ íê°ëë ì¡°ê±´ê³¼ ì ííê² ì¼ì¹í©ëë¤.
var hoisted = "foo" in this;
console.log(
`'foo' name ${
hoisted ? "is" : "is not"
} hoisted. typeof foo is ${typeof foo}`,
);
if (true) {
function foo() {
return 1;
}
}
// In Chrome:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Firefox:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Edge:
// 'foo' name is not hoisted. typeof foo is undefined
//
// In Safari:
// 'foo' name is hoisted. typeof foo is function
í¨ì ì ì¸ ëì´ì¬ë¦¬ê¸°
JavaScriptìì í¨ì ì ì¸ì ê·¸ ì ì¸ì ëë¬ì¼ í¨ìì ìµìë¶ë ì ì ë²ì(global scope)ë¡ ëì´ì¬ë ¤ì§ëë¤.
hoisted(); // logs "foo"
function hoisted() {
console.log("foo");
}
í¨ì ííì
ì ëì´ì¬ë ¤ì§ì§ ìì¼ë¯ë¡ 주ìíì¸ì:
notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function () {
console.log("bar");
};
ìì function
ì¬ì©í기
ë¤ì ì½ëë ì í a
, b
ë° c
ì ë¨ì í매ëì´ ì£¼ì´ì¡ì ë, ì´ í매ëì ë°ííë í¨ì를 ì ì¸í©ëë¤.
function calc_sales(units_a, units_b, units_c) {
return units_a * 79 + units_b * 129 + units_c * 699;
}
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4