Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33174
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Object.defineProperties |
| Date | 2017-05-03 10:59 +0200 |
| Organization | csiph.com Internet News Service |
| Message-ID | <oec665$2g1c$1@csiph.com> (permalink) |
| References | <oebtq0$2611$1@csiph.com> <objects-20170503082342@ram.dialup.fu-berlin.de> <oec3h1$2dav$1@csiph.com> <class-20170503094344@ram.dialup.fu-berlin.de> |
On 03/05/2017 10:44, Stefan Ram wrote:
> Cezary Tomczyk <cezary.tomczyk@gmail.com> 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/
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 08:36 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:14 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:59 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 12:36 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 18:47 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:18 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:14 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 19:37 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:11 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 15:26 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 16:43 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 20:23 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 22:21 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:19 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:40 +0200
Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-06 22:37 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:05 +0200
Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-07 00:29 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 10:06 +0200
Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-08 00:50 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-08 13:17 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:37 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 09:42 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 17:11 +0200
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 20:08 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:59 +0200
Re: Object.defineProperties John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-06 10:35 +0100
Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 08:32 +0200
Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:24 +0200
csiph-web