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 18:47:20 +0200 Organization: csiph.com Internet News Service Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 3 May 2017 16:47:21 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="11777"; 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:33183 On 03/05/2017 14:07, Stefan Ram wrote: > Cezary Tomczyk writes: >> 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". > > I just had this idea: You can tag each prototype with your > own tag symbol: For example, to tag the String prototype: > > |> let myTag = Symbol( "myTag" ); > |> Object.getPrototypeOf( "" )[ myTag ] = "String" > > Now, when we have any object, we can check its prototype for > our tag: > > |> Object.getPrototypeOf( "abc" )[ myTag ] > > . No one else can mess with our tag, because they don't have > the symbol! > > At least it seems to work for numbers and strings here: > > |> Object.getPrototypeOf( 1 )[ myTag ]= "Number" > > |> Object.getPrototypeOf( 2 )[ myTag ] > |< Number > > |> Object.getPrototypeOf( "abc" )[ myTag ] > |< String Interesting approach. It seems it's working. Thanks. Let's see if anyone else would have a different idea. -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility validation tool