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


Groups > comp.lang.javascript > #7599

Re: Objects comparison ?

From Scott Sauyet <scott.sauyet@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Objects comparison ?
Date 2011-10-21 05:29 -0700
Organization http://groups.google.com
Message-ID <ef11dbbe-50fc-404e-a8a5-48fc32278f2f@r1g2000yqm.googlegroups.com> (permalink)
References (10 earlier) <La6bUfEjRZnOFwLN@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> <659c944b-9ab1-46e9-a360-dd2b2f373dd4@c1g2000vbw.googlegroups.com> <OEQjhGE91unOFw7A@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> <bb93a43e-3b89-4e2d-82c6-06904737348d@a12g2000vbz.googlegroups.com> <jux+CjHb4DoOFw1Q@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>

Show all headers | View raw


John G Harris wrote:
> Scott Sauyet wrote:
>> John G Harris wrote:

>>> Also, notice that the functions dangling from Object apply to only one
>>> object and require special privileged access to the insides of objects.
>>> A function that can be used to compare two strings is not appropriate.
>
>> So is your concern only with Object as the root of the object
>> hierarchy, or does your objection apply to all constructor functions?
>> Date.parse doesn't accept a Date, only a String.  String.fromCharCode
>> accepts a number of integers.
>
> Would you be happy if it was Number.parse that turned a string into a
> Date? I'd say it would be no more inappropriate.

I'd say that was extremely inappropriate.  A function that creates
Date objects from some other structure sounds like something that
should be handled by Date.  The fact that Date is a constructor as
well as a holder for this factory function is simply a byproduct of
the fact that functions *are* objects.

Perhaps you would be happier with a construct like:

    var MyDate = {
      create: function(year, month, day /*, ... */) { /* ... */},
      parse: function(stringRep) {/* ... */},
      now: function() {/* ... */}
      /*, ... */
    };

While I have no problem with that structure, I quite like using
constructor functions for this purpose.  I don't feel as though I'm
breaking any rules or skirting around the language designs in doing
so.  Rather, I feel as though I'm using some of its better features.


> Remember, Thomas's function was not restricted to objects.

It's a dynamic language.  You don't get to control what is passed to
your public functions.  So you better be willing and able to handle
data of types other than what you would prefer.  Moreover, I assumed
he was using Object as the root of the Object hierarchy, with an eye
towards checking if all the properties of the two parameters are
equal.  So the fact that you could use this with, say, two RegExp
objects, or a Date and an Array is not necessarily a problem.  Thomas
did not try to actually present such a function.  That might not be so
simple.  But if I were to write such a function meant to be included
as part of the language, the Object constructor is by far the best
place to put it.



>> I use this pattern extensively, and wonder if you have an objection to
>> it, and if so, why:
>
>>    var MyConstructor = function() { /* ... */ };
>>    MyConstructor.prototype = { /* ... */ }
>>    MyConstructor.staticFunc1 = function() { /* ... */ };
>
> Well, yes, I don't think it's nice. Writing the code to create an object
> is one job; writing quasi-global functions for using these objects is
> another job. These jobs might be done at different times, or by
> different people.
>
> I can understand you wanting to use the constructor, but have you ever
> thought of using a separate object to hold things that are not inside
> the objects but are related to them? (In C++ it would be a namespace).

Yes, and at times I do use a separate object.  But I haven't really
seen any argument about why it's wrong to use the constructor object
for this purpose, only your distaste for doing so.  Can you say *why*
you object?  (And no, saying that these are two different jobs does
not suffice.  Any object with more than one function attached does
more than one job.)


> [Aside to others: 'static' is a bit of history from C++, maybe via Java.
> It doesn't have the obvious meaning.]

Yes, perhaps we shouldn't be using it here, if it doesn't mean much to
others.  In, say, Java, "static" members are ones that apply at a
class level, and not to individual instances.  A counter of instances
created or a next-id generator are classic examples.  It's a pretty
straightforward analog to properties that are associated with the
constructor versus those associated with the prototype.


>>> Have you considered what you would do if a 'class' has two constructors?
>>> E.g Thing and ThingClone.
>
>> I don't generally think of these static functions as operating
>> specifically on single instances that were constructed from the
>> function, so I don't think of it as an issue.  But just out of
>> curiosity, how would you accomplish this multiple constructor
>> scenario?
>
> I think you've misunderstood me. I'm talking about a bunch of objects,
> all of the same kind. Some have been created by one constructor, some by
> another constructor.

So what makes them all of the same kind?  Do they still share the same
prototype chain?  If not, I would argue against calling them the same
kind.


> For instance, by
>
>    new Thing(2057, "left-handed sproggle")
> which creates a new thing object holding the given values;
>
>    new ThingClone(t)
> where 't' must be another thing object
> which creates a new object with the same values as in t.
> (In C++ ThingClone would be called a copy constructor.)
>
> Now, which of these constructors do you hang the quasi-global functions
> from?

Well, first of all, I wouldn't do it that way.  Depending on the
implementation, I would either attach a 0-parameter `clone` function
to the prototype of Thing, or attach to the Thing constructor a
`clone` function that accepted a Thing object.  In either case, the
result would be an object constructed with the Thing constructor.

Do you really have use-cases that work this way?


I think the problem is a disagreement in mapping.  Of course the
language features do not map precisely, but if we were to try to map
Javascript to Java, I think you would map JS constructor functions to
Java constructors.  (Do I have that right?)  I would map JS
constructor functions to Java classes.  They define the data type, and
own prototypes, which contain the common features.  Of course they
also serve as the only constructors of these objects, but that to me
is secondary.  If I'm right about this disagreement, do you have a
mapping for Java classes in JS?

  -- Scott

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


Thread

Objects comparaison ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-13 20:20 +0200
  Re: Objects comparaison ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-10-13 21:47 +0300
    Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-13 21:21 +0200
      Re: Objects comparaison ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-10-13 22:40 +0300
        Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-13 21:46 +0200
          Re: Objects comparaison ? dhtml <dhtmlkitchen@gmail.com> - 2011-10-13 13:11 -0700
            Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-14 01:26 +0200
              Re: Objects comparaison ? dhtml <dhtmlkitchen@gmail.com> - 2011-10-14 12:41 -0700
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-15 21:37 +0200
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-15 15:51 -0700
                Re: Objects comparaison ? dhtml <dhtmlkitchen@gmail.com> - 2011-10-15 21:28 -0700
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-16 11:41 +0200
                Re: Objects comparaison ? dhtml <dhtmlkitchen@gmail.com> - 2011-10-16 09:05 -0700
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-16 21:59 +0200
                Re: Objects comparaison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-17 10:53 +0100
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-17 07:02 -0700
                Re: Objects comparaison ? Andreas Bergmaier <andber93@web.de> - 2011-10-17 16:27 +0200
                Re: Objects comparaison ? dhtml <dhtmlkitchen@gmail.com> - 2011-10-17 21:28 -0700
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 11:12 +0200
                Re: Objects comparaison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-18 15:23 +0100
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 19:48 +0200
                Re: Objects comparaison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-19 17:08 +0100
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-20 23:58 +0200
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-20 15:46 -0700
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 10:55 +0200
                Re: Objects comparaison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-18 15:59 +0100
                Re: Objects comparaison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 19:33 +0200
                Re: Objects comparaison ? Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-18 14:05 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-18 16:51 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-18 17:21 -0700
                Re: Objects comparaison ? Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-19 04:40 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-19 16:41 -0700
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-19 17:14 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-19 18:33 -0700
                Re: Objects comparaison ? Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-23 11:43 -0700
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-19 15:32 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-19 16:54 -0700
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-19 17:32 -0700
                Re: Objects comparaison ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-10-19 18:39 -0700
                Re: Objects comparaison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-19 16:32 +0100
                Re: Objects comparaison ? Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-19 14:18 -0700
                Re: Objects comparaison ? Antony Scriven <adscriven@gmail.com> - 2011-10-19 15:46 -0700
                Objects comparison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-20 16:28 +0100
                Re: Objects comparison ? Antony Scriven <adscriven@gmail.com> - 2011-10-20 12:23 -0700
                Re: Objects comparison ? Antony Scriven <adscriven@gmail.com> - 2011-10-20 15:04 -0700
                Re: Objects comparison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-21 15:38 +0100
                Re: Objects comparison ? Andreas Bergmaier <andber93@web.de> - 2011-10-22 19:40 +0200
                Re: Objects comparison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-23 11:49 +0100
                Re: Objects comparison ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-24 19:00 +0200
                Re: Objects comparison ? Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-21 05:29 -0700
                Re: Objects comparison ? John G Harris <john@nospam.demon.co.uk> - 2011-10-21 16:30 +0100
            Re: Objects comparaison ? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-10-18 18:06 +0300
              Re: Objects comparaison ? Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-10-18 17:43 +0200
      Re: Objects comparaison ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-14 06:11 +0200
        Re: Objects comparaison ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-10-14 08:07 +0300
          Re: Objects comparaison ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-14 07:31 +0200
            Re: Objects comparaison ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-10-14 08:59 +0300
              Re: Objects comparaison ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-14 08:36 +0200
        Re: Objects comparaison ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-14 08:35 +0200

csiph-web