Message
TypeError: "x" is (not) "y" Examples: TypeError: "x" is undefined TypeError: "x" is null TypeError: "undefined" is not an object TypeError: "x" is not an object or null TypeError: "x" is not a symbol
Error type
What went wrong?
๊ทธ๊ฒ์ ์ ํํ์ง ์์ ํํ์ด๋ค. ๊ทธ๊ฒ์ ๊ฐ๋undefined
๋ null
๊ฐ์ ๋ฐ์ํ๋ค.
๋ํ, Object.create()
๋๋ Symbol.keyFor()
์ ๊ฐ์ ๋ฉ์๋๋ ๋ฐ๋์ ์ ๊ณต๋์ด์ผํ๋ ํน๋ณํ ํํ๋ฅผ ์๊ตฌํ๋ค.
Examples
Invalid cases
// undefined and null cases on which the substring method won't work
var foo = undefined;
foo.substring(1); // TypeError: foo is undefined
var foo = null;
foo.substring(1); // TypeError: foo is null
// Certain methods might require a specific type
var foo = {}
Symbol.keyFor(foo); // TypeError: foo is not a symbol
var foo = 'bar'
Object.create(foo); // TypeError: "foo" is not an object or null
Fixing the issue
undefined ๋ null ๊ฐ์ ๊ฐ์ง null ํฌ์ธํฐ๋ฅผ ๊ณ ์น๊ธฐ ์ํด์ ์๋ ์์ ์ ๊ฐ์ด typeof ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
if (typeof foo !== 'undefined') {
// Now we know that foo is defined, we are good to go.
}