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:59:48 +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 08:59:49 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="81964"; 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:33174 On 03/05/2017 10:44, Stefan Ram wrote: > Cezary Tomczyk writes: >> 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. > > This was historically called the "[[Class]] internal slot". > > Your function gives »true« for > > isTypeOf( 3, "Number" ) > > even though »3« /is not an object/ (so it's not an object > with the [[Class]] »Number« either). > So, this might wrongly indicate that »3« is an object. But isTypeOf is not checking if specified obj is an object type. It checks expected type of obj defined in typeToCheck. This means that for 3 it gives "number" type which is correct. > After, > > class A {} > > »true« is returned from > > isTypeOf( new A(), "Object" ) > > , but beware, people also can > > class A { get [Symbol.toStringTag]() { return "B"; }} > > and then > > isTypeOf( new A(), "B" ) > > will return »true«. Here are my unit tests for isTypeOf: describe('#isTypeOf', function () { it('should return false if passed object is undefined', function () { expect(object.isTypeOf()).toBeFalsy(); }); it('should return true if object is a number type and it\'s type is passed in capital letters', function () { expect(object.isTypeOf(1, 'NUMBER')).toBeTruthy(); }); it('should return true if object is a number type', function () { expect(object.isTypeOf(1, 'number')).toBeTruthy(); }); it('should return true if object is an object type', function () { expect(object.isTypeOf({}, 'object')).toBeTruthy(); }); it('should return true if object is an array type', function () { expect(object.isTypeOf([], 'array')).toBeTruthy(); }); it('should return true if object is a string type', function () { expect(object.isTypeOf('test', 'string')).toBeTruthy(); expect(object.isTypeOf('', 'string')).toBeTruthy(); }); it('should return true if object is a boolean type', function () { expect(object.isTypeOf(true, 'boolean')).toBeTruthy(); expect(object.isTypeOf(false, 'boolean')).toBeTruthy(); }); // ES6 feature (need this verification especially for PhantomJS) if (object.isHostMethod(global, 'Symbol')) { it('should return true if object is a symbol type', function () { expect(object.isTypeOf(global.Symbol('foo'), 'symbol')).toBeTruthy(); }); } }); And they are all passing. -- Cezary Tomczyk http://www.ctomczyk.pl/