ํ์ ์ฒดํฌ
#
ํ์ ์ฒดํฌ#
typeofnull
์ ์ ์ธํ ์์ ํ์
์ ์ฒดํฌํ๋ ๊ฒ์ ๊ฐ๋ฅํ์ง๋ง ๊ฐ์ฒด์ ์ข
๋ฅ๋ฅผ ๊ตฌ๋ถํ ์ ์๋ค.
typeof ''; // stringtypeof 1; // numbertypeof NaN; // numbertypeof true; // booleantypeof []; // objecttypeof {}; // objecttypeof new String(); // objecttypeof new Date(); // objecttypeof /test/gi; // objecttypeof function () {}; // functiontypeof undefined; // undefinedtypeof null; // object (์ค๊ณ์ ๊ฒฐํจ)typeof undeclared; // undefined (์ค๊ณ์ ๊ฒฐํจ)
#
Object.prototype.toStringObject.prototype.toString ๋ฉ์๋๋ ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๋ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
โ๏ธ Number.prototype.toString, Array.prototype.toString, String.prototype.toString ์ ๊ฐ๊ฐ ์ซ์, ๋ฐฐ์ด, ์คํธ๋ง ๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ๋ฐ๊ฟ์ฃผ๋๋ฉ์๋๋ก Object.prototype.toString๊ณผ ๋ค๋ฅด๋ค.
var obj = new Object();obj.toString(); // [object Object]
console.log(Object.prototype.toString.call('')); // [object String]console.log(Object.prototype.toString.call(new String())); // [object String]console.log(Object.prototype.toString.call(1)); // [object Number]console.log(Object.prototype.toString.call(new Number())); // [object Number]console.log(Object.prototype.toString.call(NaN)); // [object Number]console.log(Object.prototype.toString.call(Infinity)); // [object Number]console.log(Object.prototype.toString.call(true)); // [object Boolean]console.log(Object.prototype.toString.call(undefined)); // [object Undefined]console.log(Object.prototype.toString.call()); // [object Undefined]console.log(Object.prototype.toString.call(null)); // [object Null]console.log(Object.prototype.toString.call([])); // [object Array]console.log(Object.prototype.toString.call({})); // [object Object]console.log(Object.prototype.toString.call(new Date())); // [object Date]console.log(Object.prototype.toString.call(Math)); // [object Math]console.log(Object.prototype.toString.call(/test/i)); // [object RegExp]console.log(Object.prototype.toString.call(function () {})); // [object Function]console.log(Object.prototype.toString.call(document)); // [object HTMLDocument]
function argumentsTest() { console.log(Object.prototype.toString.call(arguments)); // [object Arguments]}argumentsTest();
console.log(Object.prototype.toString.call(undeclared)); // ReferenceError
์ด๋ฅผ ํ์ฉํ ํ์ ๋ฐํ ํจ์ getType
function getType(target) { return Object.prototype.toString.call(target).slice(8, -1);}
console.log(getType('')); // Stringconsole.log(getType(1)); // Numberconsole.log(getType(true)); // Booleanconsole.log(getType(undefined)); // Undefinedconsole.log(getType(null)); // Nullconsole.log(getType({})); // Objectconsole.log(getType([])); // Arrayconsole.log(getType(/test/i)); // RegExpconsole.log(getType(Math)); // Mathconsole.log(getType(new Date())); // Dateconsole.log(getType(function () {})); // Function
ํ์ ๋ณ๋ก ์ฒดํฌํ๋ ํจ์
function getType(target) { return Object.prototype.toString.call(target).slice(8, -1);}
function isString(target) { return getType(target) === 'String';}
function isNumber(target) { return getType(target) === 'Number';}
function isBoolean(target) { return getType(target) === 'Boolean';}
function isNull(target) { return getType(target) === 'Null';}
function isUndefined(target) { return getType(target) === 'Undefined';}
function isObject(target) { return getType(target) === 'Object';}
function isArray(target) { return getType(target) === 'Array';}
function isDate(target) { return getType(target) === 'Date';}
function isRegExp(target) { return getType(target) === 'RegExp';}
function isFunction(target) { return getType(target) === 'Function';}
#
instanceofObject.prototype.toString์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์ข ๋ฅ๊น์ง ์๋ณํ ์ ์์ง๋ง ๊ฐ์ฒด์ ์์ ๊ด๊ณ๋ ์ฒดํฌํ ์ ์๋ค.
instanceof ์ฐ์ฐ์๋ ํผ์ฐ์ฐ์์ธ ๊ฐ์ฒด๊ฐ ์ฐํญ์ ๋ช ์ํ ํ์ ์ ์ธ์คํด์ค์ธ์ง ์ฌ๋ถ๋ฅผ์๋ ค์ค๋ค.
function Person() {}const person = new Person();
console.log(person instanceof Person); // trueconsole.log(person instanceof Object); // true
function isElement(target) { return !!(target && target instanceof HTMLElement); // ๋๋ `nodeType`์ ์ฌ์ฉํ ์๋ ์๋ค. // return !!(target && target.nodeType === 1);}
#
๋ฐฐ์ด vs ์ ์ฌ ๋ฐฐ์ด๋ฐฐ์ด์ธ์ง ์ฒดํฌํ ๋๋ Array.isArray ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
console.log(Array.isArray([])); // trueconsole.log(Array.isArray({})); // falseconsole.log(Array.isArray('123')); // false
์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด๋ length ํ๋กํผํฐ๋ฅผ ๊ฐ๊ณ , length ํ๋กํผํฐ์ ์ต๋ ๊ฐ์ 2์ 53์ ๊ณฑ - 1 ์ด๋ค. ๋ํ ๋น๋ฌธ์์ด ์ญ์ ์ ์ฌ ๋ฐฐ์ด์ด๋ค.
const isArrayLike = function (collection) { const MAX_ARRAY_INDEX = Math.pow(2, 53) - 1; const length = collection == null ? undefined : collection.length; return typeof length === 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;};
// trueconsole.log(isArrayLike([]));console.log(isArrayLike('abc'));console.log(isArrayLike(''));console.log(isArrayLike(document.querySelectorAll('li')));console.log(isArrayLike(document.getElementsByName('li')));console.log(isArrayLike({length: 0}));(function () { console.log(isArrayLike(arguments));})();
// falseconsole.log(isArrayLike(123));console.log(isArrayLike(document.querySelector('li')));console.log(isArrayLike({foo: 1}));console.log(isArrayLike());console.log(isArrayLike(null));