SyntaxError: Malformed formal parameter

Message

SyntaxError: malformed formal parameter (Firefox)

Error type

SyntaxError

๋ฌด์—‡์ด ์ž˜๋ชป ๋˜์—ˆ์„๊นŒ์š”?

์ฝ”๋“œ์—๋Š” ์ตœ์†Œํ•œ ๋‘ ๊ฐœ์˜ ์ธ์ˆ˜๊ฐ€ ์ „๋‹ฌ ๋œ Function() ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ธ์ˆ˜๋Š” ์ž‘์„ฑ์ค‘์ธ ์ƒˆ ํ•จ์ˆ˜์˜ ์†Œ์Šค์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.  ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ ์ƒˆ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜ ๋ชฉ๋ก์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ธ์ˆ˜ ๋ชฉ๋ก์ด ์–ด๋”˜๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•„๋งˆ๋„ ์‹ค์ˆ˜๋กœ ์ธ์ˆ˜ ์ด๋ฆ„์œผ๋กœ if ๋˜๋Š” var์™€ ๊ฐ™์€ ํ‚ค์›Œ๋“œ๋ฅผ ์„ ํƒํ–ˆ๊ฑฐ๋‚˜ ์ธ์ˆ˜ ๋ชฉ๋ก์— ๊ฐˆ๊ณณ ์žƒ์€ ๊ตฌ๋‘์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ์‹ค์ˆ˜๋กœ ์ˆซ์ž๋‚˜ ๊ฐœ์ฒด์™€ ๊ฐ™์€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ „๋‹ฌํ•œ ๊ฒƒ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜, ๋‚ด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋์–ด. ๊ทผ๋ฐ ์™œ ๋ง ์•ˆํ•ด์คฌ์–ด?

ํ‹€๋ฆผ ์—†์ด ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€์˜ ํ‘œํ˜„์€ ๋‹ค์†Œ ์ด์ƒํ•œ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. "๊ณต์‹ ๋งค๊ฐœ ๋ณ€์ˆ˜"๋Š” "ํ•จ์ˆ˜ ์ธ์ˆ˜"๋ฅผ ๋งํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” "์ž˜๋ชป๋œ ํ˜•์‹์˜ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค"๋ผ๊ณ  ํ‘œํ˜„ ํ•ฉ๋‹ˆ๋‹ค.

Examples

Invalid cases

var f = Function('x y', 'return x + y;');
// SyntaxError (missing a comma)

var f = Function('x,', 'return x;');
// SyntaxError (extraneous comma)

var f = Function(37, "alert('OK')");
// SyntaxError (numbers can't be argument names)

Valid cases

var f = Function('x, y', 'return x + y;');  // correctly punctuated

var f = Function('x', 'return x;');

// if you can, avoid using Function - this is much faster
var f = function(x) { return x; };

See also