Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33226

Re: Object.defineProperties

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Object.defineProperties
Date 2017-05-05 20:11 +0200
Organization PointedEars Software (PES)
Message-ID <1592229.TLkxdtWsSY@PointedEars.de> (permalink)
References <oebtq0$2611$1@csiph.com> <objects-20170503082342@ram.dialup.fu-berlin.de> <oec3h1$2dav$1@csiph.com> <1943190.NgBsaNRSFp@PointedEars.de> <oeid9d$1ag1$1@csiph.com>

Show all headers | View raw


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();
  }
 
>>> 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” ;-)).
 
>> typeof function () {} === "function", so that is not OK in theory;
>> however, one might decide that it is not OK to modify function objects.
> 
> Functions are objects so they can be extended like any other objects.

They also have built-in, non-inherited, read-only properties.  I augment 
function objects only as a last resort (currently, to tag them as user-
defined).
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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