Path: csiph.com!.POSTED.204-73-239-109.cust.centrio.cz!not-for-mail From: Cezary Tomczyk Newsgroups: comp.lang.javascript Subject: Re: Object.defineProperties Date: Sat, 6 May 2017 23:05:55 +0200 Organization: csiph.com Internet News Service Message-ID: References: <1943190.NgBsaNRSFp@PointedEars.de> <1592229.TLkxdtWsSY@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 6 May 2017 21:05:56 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="19618"; mail-complaints-to="admin@kev009.com" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: Xref: csiph.com comp.lang.javascript:33242 On 06/05/2017 22:37, Stefan Weiss wrote: > Cezary Tomczyk wrote: >> if (typeToCheck === 'array' && Array.isArray(obj) >> || typeToCheck === 'null' && objectType === 'object' >> || typeToCheck === 'object' && objectType === 'object' > > So any object except an Array tests positive for "null" - is that intentional? > > isTypeOf({}, "null") → true > > Also, if you're going to such lengths, you might as well include the > "function" type in the conditional. > > On the whole, I'm not convinced that this function is very useful in > practice. I can't think of many situations where I would consider `false` > and `new Boolean(false)` as having the same "type". They're certainly not > interchangable. The `typeof` operator and `Array.isArray()` are usually all > that's needed. You're right. I've simplified it. Aparat of 'typeof' operator, 'Array.isArray()' I need to cover 'null' case. function isTypeOf(obj, typeToCheck) { var type; if (arguments.length < 2) { throw new Error('[isTypeOf] requires two arguments'); } type = typeToCheck.toLowerCase(); if (type === 'null' && obj === null) { return 'null'; } if (type === 'array' && Array.isArray(obj)) { return 'array'; } return typeof obj === type; } -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility validation tool