Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Object.defineProperties Date: Sat, 06 May 2017 16:43:23 +0200 Organization: PointedEars Software (PES) Lines: 117 Message-ID: <2032926.ElGaqSPkdT@PointedEars.de> References: <1943190.NgBsaNRSFp@PointedEars.de> <1592229.TLkxdtWsSY@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1494081803 16642 eJwFwQkBwDAIA0BLfIEiJ6PFv4TdwVNzKhIZWOyZhUgZW9fv6+s0lqPVnk5I8aOYRzGGB/YDFqAQug== (6 May 2017 14:43:23 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 6 May 2017 14:43:23 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQERADEIAzBL8EC5ydlD61/Ckgo4phOFLJU0KJIzUPYcJuQ47PvbrWO+uxVD3w7tJTJmBTU/a4PiAYwcFsQ= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On 05/05/2017 20:11, Thomas 'PointedEars' Lahn wrote: >> Cezary Tomczyk wrote: >>> On 05/05/2017 18:14, Thomas 'PointedEars' Lahn wrote: >>>> Cezary Tomczyk wrote: >>> [...] >>>>> 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. >>>> I hope you have optimized that somewhat. >>> I can't find a reliable way at the moment. Perhaps I should combine few >>> methods, but that might be an overengineering. >> >> To begin with, you should have >> >> var _toString = {}.toString; >> >> function isTypeOf(obj, typeToCheck) { >> if (arguments.length < 2) { >> return false; >> } >> return _toString.call(obj).slice(8, -1).toLowerCase() === >> typeToCheck.toLowerCase(); >> } > > Tiny optimisation, but that's fine. :-) Tiny optimization with big impact, I think. It saves - 1 lookup along the scope chain up to the global object for “Object” *every time the function is called* - 2 lookups along the prototype chain each for “prototype” and “toString” *every time the function is called* and removes the dependency on a potentially non-initial value of “Object.prototype” and “Object.prototype.toString”. >>>>> 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. >>>> typeof [] === "object", so that is OK. (common misconception) >>> >>> In a sense of that [] is an object - yes, but I'd prefer to know that >>> tested object is in fact an Array type. Operator instanceof looks >>> promising, but I need to check it anyway. >> >> Array.isArray() exists (since ECMAScript Ed. 5). For general and own >> (array) types, I am using “instanceof” or “constructor”. Most of the >> time, though, duck typing instead (“quack” ;-)). > > Ah, right, I forgot about Array.isArray() :) Be sure to emulate it if it is not there. > Finally I rewrote it a bit: > > var _toString = {}.toString; > > function isTypeOf(obj, typeToCheck) { > var objectType; > > if (arguments.length !== 2) { This change does not make sense to me. “< 2” was clearer. > return false; > } > > objectType = typeof obj; IMO, DRY demands declaration and initialization in one statement, if possible. > if (typeToCheck === 'array' && Array.isArray(obj) > || typeToCheck === 'null' && objectType === 'object' > || typeToCheck === 'object' && objectType === 'object' > || typeToCheck === 'number' && objectType === 'number' > || typeToCheck === 'string' && objectType === 'string' > || typeToCheck === 'boolean' && objectType === 'boolean' > || typeToCheck === 'undefined' && objectType === 'undefined' > || typeToCheck === 'symbol' && objectType === 'symbol' I would use parentheses to make clear what conditions belong together. > ) { > return true; > } > > return _toString.call(obj).slice(8, -1).toLowerCase() === > typeToCheck.toLowerCase(); > } The question remains: What is being *gained* by this comparably inefficient precision instead of duck typing? _Not_ all things that *can* be done also *should* be done. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.