Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!newsfeed10.multikabel.net!multikabel.net!newsfeed20.multikabel.net!amsnews11.chello.com!eweka.nl!lightspeed.eweka.nl!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <8938713.SEqChMirdb@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 12 Nov 2011 19:31:42 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit 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 MIME-Version: 1.0 Lines: 130 NNTP-Posting-Date: 12 Nov 2011 19:31:43 CET NNTP-Posting-Host: 66483151.newsspool3.arcor-online.net X-Trace: DXC=l>e\_;_Hh`df1oJaJ0@dmgMcF=Q^Z^V3h4Fo<]lROoRa8kF 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] or ; 2004-05-20 [3] or ; 2007-11-30 [2] [3] ; 2007 -- When all you know is jQuery, every problem looks $(olvable).