Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8225 > unrolled thread
| Started by | Gene Wirchenko <genew@ocis.net> |
|---|---|
| First post | 2011-11-10 20:02 -0800 |
| Last post | 2011-11-14 10:20 +0100 |
| Articles | 8 on this page of 28 — 8 participants |
Back to article view | Back to comp.lang.javascript
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
Page 2 of 2 — ← Prev page 1 [2]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-12 18:14 +0000 |
| Message-ID | <Wc6EIfJHervOFwIp@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8259 |
On Sat, 12 Nov 2011 at 16:04:01, in comp.lang.javascript, Denis McMahon
wrote:
<snip>
>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, although he won't actually go so far as to post "the right way",
>because he much prefers to criticise the coding of others than to expose
>his own code to critical comment.
Speak of the devil and up he pops. Thank you for your kind comments.
First off : there is no "the right way". There are many ways and several
of them are equally clean; the rest have various amounts of messiness
built in, and some are downright perverse.
In your example you build a Commercial object by doing :
var factory = new Commercial();
factory.setLocation("13 West Street");
factory.setValue("750000");
factory.setZoning("Light Industry");
factory.setUse("Vehicle Bodyshop");
My view is that it's simpler and easier to read to do
var factory = new Commercial
( "13 West Street",
"750000",
"Light Industry",
"Vehicle Bodyshop" );
If you have many Commercial objects to create you have to remember to
type four factory.setX(...) every time. Forget one line once and
you've got a bug and a lot of code to look through to find it.
John
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-13 19:34 +0000 |
| Message-ID | <4ec01bd4$0$28586$a8266bb1@newsreader.readnews.com> |
| In reply to | #8265 |
On Sat, 12 Nov 2011 18:14:31 +0000, John G Harris wrote: > Speak of the devil and up he pops. Thank you for your kind comments. I wasn't actually talking about you. But if you recognise yourself in the description ;) > In your example .... > My view is that it's simpler and easier to read to do > > var factory = new Commercial > ( "13 West Street", > "750000", > "Light Industry", > "Vehicle Bodyshop" ); So you'd prefer http://www.sined.co.uk/tmp/objects2.htm then. (I thought I posted this or something like it yesterday, but it hasn't appeared in my newsfeed yet) Rgds Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-11-12 19:31 +0100 |
| Message-ID | <8938713.SEqChMirdb@PointedEars.de> |
| In reply to | #8259 |
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).
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-13 20:33 -0800 |
| Message-ID | <3g61c7hl54v1vs6gaktpe19ues2ijg1djd@4ax.com> |
| In reply to | #8259 |
On 12 Nov 2011 16:04:01 GMT, Denis McMahon <denismfmcmahon@gmail.com>
wrote:
[snip]
>I think the OP wants to be able to assign different locations and values
>to different commercial properties.
Yes.
[snip]
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-12 16:52 +0000 |
| Message-ID | <SO6lMzFGRqvOFw53@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8253 |
On Sat, 12 Nov 2011 at 12:53:57, in comp.lang.javascript, Elegie wrote:
>On 11/11/2011 05:02, Gene Wirchenko wrote :
>
>Hi,
>
>> 1) How should I be doing it?
>
>Using your example, what you probably are looking for is:
>
>---
>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");
>---
<snip>
Your Commercial prototype is not a good idea. How, for instance, would a
new Commercial object get a property location that isn't Kamloops?
John
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | Elegie <elegie@anonymous.invalid> |
|---|---|
| Date | 2011-11-12 20:49 +0100 |
| Message-ID | <4ebecdbc$0$15934$426a34cc@news.free.fr> |
| In reply to | #8261 |
On 12/11/2011 17:52, John G Harris wrote : Hello John, > Your Commercial prototype is not a good idea. How, for instance, would a > new Commercial object get a property location that isn't Kamloops? Actually I did not think about it at all, I simply wanted to introduce the OP to the concept of prototype chain, and have him reflect about it a bit... Regards, Elegie.
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-13 20:21 -0800 |
| Message-ID | <cp51c75ne7qjvu2u952p1sifgg91l0vvb0@4ax.com> |
| In reply to | #8269 |
On Sat, 12 Nov 2011 20:49:14 +0100, Elegie <elegie@anonymous.invalid>
wrote:
>On 12/11/2011 17:52, John G Harris wrote :
>
>Hello John,
>
>> Your Commercial prototype is not a good idea. How, for instance, would a
>> new Commercial object get a property location that isn't Kamloops?
>
>Actually I did not think about it at all, I simply wanted to introduce
>the OP to the concept of prototype chain, and have him reflect about it
>a bit...
I already have been doing so. That is why I wrote my test code
in the first place.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Elegie <elegie@anonymous.invalid> |
|---|---|
| Date | 2011-11-14 10:20 +0100 |
| Message-ID | <4ec0dd61$0$8868$426a74cc@news.free.fr> |
| In reply to | #8302 |
On 14/11/2011 05:21, Gene Wirchenko wrote : Hi, >> Actually I did not think about it at all, I simply wanted to introduce >> the OP to the concept of prototype chain, and have him reflect about it >> a bit... > > I already have been doing so. That is why I wrote my test code > in the first place. Reading you code in the first place induced me to think that while you were aware about inheritance (a way to structurally link objects together, so as to share properties and methods), you did not know anything about prototyped inheritance. In short: - you did not use any "prototype", but rather referred to some property named "template", about which I know nothing (my knowledge may be at fault though, because I only know the Third Edition of ECMAScript), - you (apparently) tried to relate together two objects which shared common property identifiers, which could obviously only result in some properties shadowing the others, - you did not know what the "new" keyword would do, i.e. the difference between the [[call]] and the [[construct]] behaviors of a function. Consequently, I decided to give you an example that would let you discover the concept of prototype chain, and in addition I pointed you out to relevant parts of the specification, which could help you get a more solid knowledge about it. The way that I participate in this group has many facets (as can be ascertained by reading my previous posts on this group): - with beginners - and I put you in that category, as you have yourself qualified as such - I always do "tutoring javascript", i.e. point out to specifications and give working examples, but simplified to the point that the OP can understand it, and build on them to advance further on the javascript path, - with proficient posters, I provide scripts solving problems, but not optimized (i.e. weak API, straightforward design), because I believe that context matters and that the OP has the skills to adjust the script to his / her needs, - with experts, I usually do not provide scripts (because they would write them as easily as I do), but discuss aspects of the problem, giving my intuitions and reasonings. I do that very rarely though, because there are few "expert" topics which are of interest to me. So, to sum it up, the example I provided was intentionally simple, since it was a product of "tutoring javascript". The problem that John and Denis have mentioned was probably the natural next step in understanding the prototype chain, and it is not a complicated one to solve, provided you really understand how the prototype chain works. The end-result, however, is not satisfying from a pattern point of view, and should lead you to ponder thoughtfully what should and should not be done with javascript inheritance (and what other alternate patterns you could use to better structure your code). I am sorry that you do not appreciate my contributions. I will therefore put you at ease and refrain from posting them in your topics, so that you can concentrate on more useful answers. I wish you to enjoy your stay in that newsgroup, and to soon become proficient with the technology. Regards, Elegie.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.javascript
csiph-web