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 12:36:12 +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: 8bit Injection-Date: Wed, 3 May 2017 10:36:14 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="87233"; 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:33176 On 03/05/2017 11:24, Stefan Ram wrote: > Cezary Tomczyk writes: >> And they are all passing. > > Yes, but the OP question was about detecting > whether something is an /object/. To be precise I was asking rather about why polyfill of Object.defineProperties https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill doesn't allow if (typeof obj !== 'object' || obj === null) throw new TypeError('bad obj'); to add properties to the other objects like function or array. :-) The native built-in Object.defineProperties does allow it. > And, my previous example was not to the point! > This is more like it: > > let a = new String( "abc" ); > > console.log( Object.prototype.toString.call( a ) ); > > Object.defineProperty > ( a, Symbol.toStringTag, { get: function() { return "Number"; }}); > > console.log( Object.prototype.toString.call( a ) ); > > This will print > > [object String] > [object Number] > > even though ťaŤ is still a string. Console: > >> a > < String { "abc", 4 more... } Interesting. I didn't know about Symbol.toStringTag (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag). Seems typeof operator returns "object" before changing and after changing string description of an object. So, I can't trust what's returned by Object.prototype.toString.call(obj). However, if I want to check what type is an object then I can use typeof operator, but it's not quite thing I want as the typeof for object type array give me "object". From one side it is fine as the array is an object in fact, but what I wanted is to check what's the real type of an object. Object.prototype.toString.call() gives me what I want, but only under condition that the default string description of an object has not changed. -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility validation tool