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: Wed, 3 May 2017 10:14:24 +0200 Organization: csiph.com Internet News Service Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 3 May 2017 08:14:25 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="79199"; 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:33171 On 03/05/2017 09:24, Stefan Ram wrote: > Cezary Tomczyk writes: >> Native built-in Object.defineProperties method allows me to add >> properties to a function as the function is an object. > > The Web gives: > > function isObject(val) { > if (val === null) { return false;} > return ( (typeof val === 'function') || (typeof val === 'object') ); > } > > . The above might not work with host objects, which might > have even other types. So another suggestion from the Web is: > > function isObject(obj) { > return obj === Object(obj); > } > > The directly above might be used by underscore.js. > > Other observations: > > Object.getPrototypeOf does /not/ throw when called with an object. I use: function isTypeOf(obj, typeToCheck) { if (arguments.length < 2) { return false; } return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() === typeToCheck.toLowerCase(); } which gives me more precisely a type of object. The Object.defineProperties says about object, but function, array are also objects. That's why: if (typeof obj !== 'object' || obj === null) throw new TypeError('bad obj'); looks like a bug to me. -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility validation tool