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


Groups > comp.lang.javascript > #28102

Re: What technique I use in functions?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: What technique I use in functions?
Date Tue, 16 Jun 2015 00:08:55 +0200
Organization PointedEars Software (PES)
Lines 73
Message-ID <2007292.BycLjHjpcD@PointedEars.de> (permalink)
References <0714ecc6-40b5-4500-b2f6-324bf75692b6@googlegroups.com> <objects-20150615015424@ram.dialup.fu-berlin.de> <2878487.iBXhT1CByJ@PointedEars.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1434406307 25460 eJwNysEBwCAIA8CVQE0i60Bh/xHsvQ+bztIheDAYReYuftZa1U5X5lXZGCOtytaQ+Ev3DWgeJG4RaQ== (15 Jun 2015 22:11:47 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Mon, 15 Jun 2015 22:11:47 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwFwQkBwDAIA0BLUMInJ2XFv4TduYXGJMIDvr5J43bZfoeA84GzRypmuAoK37y17Q4OlCaF29dKgaTkD3qGFhQ=
Cancel-Lock sha1:MzmXHwvR0VDKBAeSzD6HxY5d/i4=
X-NNTP-Posting-Host eJwNyMkBwCAIBMCWQmRZKYdD+i9BP/MYLBMrqsEUgzHGC1gj2n35DuHz8NPc05r/mVCpzM1k+QUOthEe
Xref csiph.com comp.lang.javascript:28102

Show key headers only | View raw


Thomas 'PointedEars' Lahn wrote:

> Stefan Ram wrote:
>>   . We have not used constructors, so »newCounter.constructor.name«
>>   is »Object«.

Only partially true.  Historically, Function instances as the one referred 
by “newCounter.constructor” did not have a “name” property per 
Specification.  This property was implementation-dependent, namely it had 
been introduced as a feature of Netscape JavaScript that was later 
implemented in Mozilla JavaScript, Opera ECMAScript, and Google V8 
JavaScript as well; but according to test results not, for example, in 
Microsoft JScript:

<http://PointedEars.de/es-matrix/?filter=Function.prototype.name>

This is going to change with ECMAScript Edition 6/2015 where the “name” 
property of Function instances is specified (section 19.2.4.2 of the final 
draft).

> Which, almost needless for me to say, is _not_ a good idea.  It is useful
> to be able to tell apart types of objects.  The Google V8 JavaScript 
> console and Chromium Dev Tools inspector use the prototype chain to get 
> the type, and the “constructor” property is an efficient way to do that in 
> one’s own code:
> 
>   if (!(this instanceof this.constructor))
>   {
>     throw new Error("Method called on incompatible object");
>   }

This particular suggestion is not completely wrong, but may be misleading.  
Usually 

  (this instanceof this.constructor) === true

so you cannot use that to test the object type /per se/.  In an instance or 
prototype method you would need a closure to hold a reference to the 
original constructor.  I thought it would work for (i.e., exclude) data 
objects (“Object.create(null))”), but in Chromium:

| >>> Foo.prototype.bar.call(Object.create(null))
| Uncaught TypeError: Expecting a function in instanceof check, but got 
| undefined
|   at Foo.bar (<anonymous>:2:80)
|   at <anonymous>:2:19
|   at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
|   at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
|   at Object.InjectedScript.evaluate (<anonymous>:694:21)

This is probably due to section “12.9.4 Runtime Semantics: 
InstanceofOperator(O, C)” in the ES6/2015 final draft, and corresponding 
sections in earlier Editions.

The remedy appears to be

  if (!(this.constructor && this instanceof this.constructor))

and for strict mode

  if (!(this && this.constructor && this instanceof this.constructor))

But again, this would only prevent execution with data objects or objects 
where the constructor property has been modified inappropriately.

Also, TypeError is the more appropriate exception type here, although it 
appears to be a good idea to define one’s own exception type for this case.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


Thread

What technique I use in functions? Angel Rodriguez <cimadelmundo3000@gmail.com> - 2015-06-14 14:38 -0700
  Re: What technique I use in functions? Angel Rodriguez <cimadelmundo3000@gmail.com> - 2015-06-14 18:16 -0700
    Re: What technique I use in functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-15 23:31 +0200
      Re: What technique I use in functions? Angel Rodriguez <cimadelmundo3000@gmail.com> - 2015-06-15 17:54 -0700
        Re: What technique I use in functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-16 08:49 +0200
  Re: What technique I use in functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-15 23:31 +0200
    Re: What technique I use in functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-16 00:08 +0200

csiph-web