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


Groups > comp.lang.javascript > #29562

Re: Newbie trying to understand object oriented programming

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Newbie trying to understand object oriented programming
Date 2016-02-06 16:01 +0100
Organization PointedEars Software (PES)
Message-ID <2195610.4cT6EPdiQK@PointedEars.de> (permalink)
References <664696a0-3ce3-484c-aeaf-2fdf45781bb4@googlegroups.com> <9cydnYY0F6n_AyjLnZ2dnUU7-fOdnZ2d@westnet.com.au> <toObject-20160206122259@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

> RobG <rgqld@iinet.net.au> writes:
>> Within a function, *this* is assigned the value of an object
> 
>   That seems to be true.

But it is only true in non-strict mode:

| > typeof (function () { "use strict"; return this; }).call(2)
| < "number"

>   It can be exploited to wrap a value into an object!

What you are doing below is conversion, not “wrapping”.  It is far simpler 
to call the function for a built-in type with the value to be converted as 
argument:
 
> |< function toObject(){ return this; }
> |> undefined
> 
> |< typeof 2
> |> "number"
> 
> |< typeof toObject.call( 2 )
> |> "object"

  new Number(2)

does the same, but also works in strict mode.  Still, creating such objects 
is unnecessary and recommended against as they cannot be modified and 
comparing against references to them tend to yield counter-intuitive 
results:

  var zeroValue = 0;
  var zeroObjectRef = new Number(0);

  if (zeroValue)
  {
    /* code here is not executed as 0 is converted to false */
  }

  if (zeroObjectRef)
  {
    /* code here is executed as an object reference is converted to true */
  }

Therefore, JSHint issues a warning about that by default.

>> You then create a *colour* property of this new instance and assign it
>> the value "brown".
> 
>   BTW, ECMAScript uses the spelling »color« IIRC.

There is no built-in “color” or “colour” property in ECMAScript.  You are 
confusing the DOM (CSS) API with one family of programming languages that it 
can be used with (ECMAScript implementations).  The OP is *not* using the 
DOM (CSS) API; they are creating a user-defined object, so they are free to 
use any property name in any language in any spelling that they want 
(“colour” is the spelling in British English), it should only refer to what 
they are storing as value (and it helps to choose an English name so that 
many people can understand your code easily, and you are less likely to have 
to use the bracket property accessor syntax).

-- 
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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-05 06:55 -0800
  Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-05 18:56 +0000
  Re: Newbie trying to understand object oriented programming Tim Slattery <tim@risingdove.com> - 2016-02-05 14:07 -0500
  Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-05 17:34 -0300
    Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-05 19:10 -0800
      Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 08:33 +0000
        Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 08:24 -0800
          Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 18:48 +0000
            Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 12:28 -0800
              Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 21:01 +0000
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-07 15:23 -0800
                Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-08 11:45 -0300
            Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-08 13:39 -0800
              Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-11 08:09 -0800
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-11 12:22 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-12 11:09 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-12 13:34 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-13 14:40 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 10:18 -0800
                Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-14 21:27 +0100
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-14 10:13 -0400
                Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-05-14 16:47 +0100
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:05 -0400
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-14 10:08 -0700
                Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:02 -0400
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:11 -0700
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-15 17:03 +0100
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:20 -0700
                Re: Newbie trying to understand object oriented programming Gene Wirchenko <genew@telus.net> - 2016-05-16 11:55 -0700
                Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-16 21:39 +0200
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-17 07:30 -0700
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-17 15:34 +0100
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 17:50 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-14 11:15 +0000
                Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 11:03 -0800
                Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-15 20:18 +0000
                Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 16:54 -0800
  Re: Newbie trying to understand object oriented programming Danny <dann90038@gmail.com> - 2016-02-05 15:00 -0800
  Re: Newbie trying to understand object oriented programming RobG <rgqld@iinet.net.au> - 2016-02-06 17:18 +1000
    Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-06 16:01 +0100
    Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-07 04:47 -0800
      Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-07 13:16 +0000
      Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-07 17:29 +0000
        Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-08 08:53 -0800
          Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-08 17:33 +0000
  Re: Newbie trying to understand object oriented programming Joao Rodrigues <jr@none.com> - 2016-02-06 11:56 -0200

csiph-web