ReferenceError: "x" is not defined
๋ฉ์์ง
ReferenceError: "x" is not defined
์๋ฌ ํ์
๋ฌด์์ด ์๋ชป๋์์๊น?
์กด์ฌํ์ง ์๋ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ ๊ณณ์ด ์์ต๋๋ค. ์ด ๋ณ์๋ ์ ์ธ๋์ด์ผ ํฉ๋๋ค. ๋๋, ํ์ฌ ์คํฌ๋ฆฝํธ๋ scope ์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋๋ก ํด์ผํฉ๋๋ค.
Note: ๋ผ์ด๋ธ๋ฌ๋ฆฌ(์๋ฅผ ๋ค๋ฉด jQuery์ ๊ฐ์)์ ๋ก๋ฉ์, ๋ฐ๋์ ์ฝ๋์์ "$"์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ณ์์ ์ ๊ทผํ๊ธฐ ์ด์ ์ ์ํ๋์ด์ผ ํฉ๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ก๋ฉํ๋ <script>
ํ๊ทธ๊ฐ ๊ทธ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ณด๋ค ์์ ์์นํ๋๋ก ํ์ธ์.
์
์ ์ธ๋์ง ์์ ๋ณ์
foo.substring(1); // ReferenceError: foo is not defined
"foo" ๋ณ์๋ ์ด๋์๋ ์ ์ธ๋์ง ์์์ต๋๋ค. String.prototype.substring()
๋ฉ์๋๊ฐ ์๋ํ๋๋ก ํ๊ธฐ ์ํด์๋ ๋ฌธ์์ด์ ํ์๋ก ํฉ๋๋ค.
var foo = "bar";
foo.substring(1); // "ar"
์๋ชป๋ ์ค์ฝํ
๋ณ์๋ ํ์ฌ์ ์คํ ํ๋ฆ ๋ด์์ ์ด์ฉ ๊ฐ๋ฅํด์ผํฉ๋๋ค. ํจ์ ๋ด๋ถ์ ์ ์๋ ๋ณ์๋ ๋ค๋ฅธ ์ธ๋ถ์ ํจ์์์๋ ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ทธ ๋๋ฌธ์, ๋ณ์๋ ํจ์์ ์ค์ฝํ ๋ด๋ถ์์๋ง ์ ์ ๋ฉ๋๋ค.
function numbers () {
var num1 = 2,
num2 = 3;
return num1 + num2;
}
console.log(num1); // ReferenceError: num1 is not defined
๊ทธ๋ฌ๋, ํจ์๋ ๋ชจ๋ ๋ณ์์ ์ ์๋ ์ค์ฝํ ์์ ์ ์๋ ํจ์์ ์ ๊ทผํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์, ์ ์ญ์ผ๋ก ์ ์๋ ํจ์๋ ์ ์ญ์ ์ ์๋ ๋ชจ๋ ๋ณ์์๋ ์ ๊ทผํ ์ ์์ต๋๋ค.
var num1 = 2,
num2 = 3;
function numbers () {
return num1 + num2;
}
console.log(num1); // 2