Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8267
| Message-ID | <8938713.SEqChMirdb@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2011-11-12 19:31 +0100 |
| Subject | Re: JavaScript: Object Definition Extension |
| Newsgroups | comp.lang.javascript |
| References | <887pb7titjugv9u6j6tiv9cjpdv8t6div7@4ax.com> <4ebe5e58$0$3173$426a74cc@news.free.fr> <4ebe98f1$0$28586$a8266bb1@newsreader.readnews.com> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Denis McMahon wrote:
> Elegie wrote:
>> function RealEstate(PropertyLocation, PropertyValue) {
>> this.PropertyLocation=PropertyLocation;
>> this.PropertyValue=PropertyValue;
>> }
>>
>> function Commercial (PropertyZoning, PropertyUse) {
>> this.PropertyZoning=PropertyZoning;
>> this.PropertyUse=PropertyUse;
>> }
>>
>> Commercial.prototype=new RealEstate("Kamloops",150000);
>>
>> var Prop1=new Commercial("C-1","light commercial"); ---
>
> Wouldn't that mean that all Commercial properties have the same location
> and value?
>
> I think the OP wants to be able to assign different locations and values
> to different commercial properties.
>
> eg: http://www.sined.co.uk/tmp/objects.htm
>
> I'm sure that someone will be along soon to tell us that I've done it
> wrong,
You have done it wrong.
> although he won't actually go so far as to post "the right way",
The right way is to _not_ have Commercial instances inherit from the same
(empty Property) instance as both of your codes did. So you have certainly
not avoided the mistake that you mention above; instead, you have added
other mistakes on top of it. One, because there are no methods to refer to
them, the variable declarations in `Property' are mystical incantations; the
code will "work" exactly the same without them.
The intention of the original code obviously was to have Commercial
instances inherit from RealEstate.prototype, i. e. Commercial was intended
to be a specialization of RealEstate. In your nonsense code, Commercial
ends up to be a specialization of Property, and RealEstate is missing.
> because he much prefers to criticise the coding of others than to expose
> his own code to critical comment.
[I am sure someone has told you before that you are an obnoxious ignorant
who does not have a first idea about these programming languages or the APIs
they can be used with (see also your one dysfunctional, and the other
unnecessarily inefficiant and incompatible checkbox toggle suggestions
earlier).]
The first thing to make this work without changing too much of the original
code is get the identifiers right. Property names should be concise, and
start lowercase:
function RealEstate(location, value)
{
this.location = location;
this.value = value;
}
function Commercial(zoning, use)
{
this.zoning = zoning;
this.use = use;
}
Then one needs to set up the prototype chain properly, i. e. let Commercial
inherit from RealEstate.prototype, not from a single RealEstate instance.
This is facilitated with a method that Lasse Reichstein Nielsen introduced
here years ago as clone() [1], that I named inheritFrom() afterwards (as it
does not really clone) [2], and that Douglas Crockford eventually came to
name object() (at least in his interesting, but slightly erroneous
JavaScript introduction at Yahoo! [3]). In its non-optimized form (see
jsx.object.inheritFrom() for the optimization [4]), it is:
function inheritFrom(proto)
{
function Dummy() {}
Dummy.prototype = proto;
return new Dummy();
}
Commercial.prototype = inheritFrom(RealEstate.prototype);
With this approach, a Dummy instance is defined as the prototype of
Commercial instances that does nothing else than to inherit from
RealEstate.prototype, thus establishing the prototype chain link between
Commercial instances and RealEstate.prototype:
(new Commercial()) → (Commercial.prototype === new Dummy())
→ RealEstate.prototype
But as Commercial.prototype.constructor is now Dummy implicitly, it would be
well that that be fixed (this part you got right):
Commercial.prototype.constructor = Commercial;
Afterwards the Dummy instance that Commercial.prototype refers to can be
refined, serving as the prototype of Commercial instances.
(Function.prototype.extend() does all that in JSX:object.js [4]. It allows
this inheritance approach to be written
Commercial.extend(RealEstate, extensions);
as you would in natural language. That also adds a `_super' property to
Commercial.prototype to refer to RealEstate.prototype, and it adds a
`_super' property to Commercial to refer to RealEstate, among other things.)
The latter which then can be created with
var prop1 = new Commercial("C-1", "light commercial");
PointedEars
______
[1]
<https://groups.google.com/forum/#!msg/comp.lang.javascript/CT4_IGmUob8/Gi4CKqG7WYoJ>
or <news:u0ybgfdr.fsf@hotpop.com>; 2004-05-20
[3]
<https://groups.google.com/forum/#!topic/comp.lang.javascript/HMJosLXN9LQ>
or <news:4750707D.9010907@PointedEars.de>; 2007-11-30
[2]
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fobject.js>
[3] <http://www.youtube.com/watch?v=v2ifWcnQs6M>; 2007
--
When all you know is jQuery, every problem looks $(olvable).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-10 20:02 -0800
Re: JavaScript: Object Definition Extension "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-11-11 08:49 +0000
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-11 05:40 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-11 20:11 +0000
Re: JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-11 21:02 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-12 16:46 +0000
Re: JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-14 12:26 -0800
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-15 06:49 -0800
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-15 09:47 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-15 15:43 +0000
Re: JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-15 10:31 -0800
Re: JavaScript: Object Definition Extension Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-11-15 23:35 +0100
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-16 20:42 +0000
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-12 09:35 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-13 11:40 +0000
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-13 11:53 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-14 16:19 +0000
Re: JavaScript: Object Definition Extension beegee <bgulian@gmail.com> - 2011-11-15 10:02 -0800
Re: JavaScript: Object Definition Extension Elegie <elegie@anonymous.invalid> - 2011-11-12 12:53 +0100
Re: JavaScript: Object Definition Extension Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-12 16:04 +0000
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-12 18:14 +0000
Re: JavaScript: Object Definition Extension Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-13 19:34 +0000
Re: JavaScript: Object Definition Extension Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-12 19:31 +0100
Re: JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-13 20:33 -0800
Re: JavaScript: Object Definition Extension John G Harris <john@nospam.demon.co.uk> - 2011-11-12 16:52 +0000
Re: JavaScript: Object Definition Extension Elegie <elegie@anonymous.invalid> - 2011-11-12 20:49 +0100
Re: JavaScript: Object Definition Extension Gene Wirchenko <genew@ocis.net> - 2011-11-13 20:21 -0800
Re: JavaScript: Object Definition Extension Elegie <elegie@anonymous.invalid> - 2011-11-14 10:20 +0100
csiph-web