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 | 20 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 1 of 2 [1] 2 Next page →
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-10 20:02 -0800 |
| Subject | JavaScript: Object Definition Extension |
| Message-ID | <887pb7titjugv9u6j6tiv9cjpdv8t6div7@4ax.com> |
Dear JavaScripters:
The following code is meant is extend an object definition. The
code works. My question is whether it works best. Is the way that I
have initialised the properties in Commercial that are from RealEstate
the best way to do it? I have a feeling that I might/should
initialise them using the RealEstate constructor, but I do not know
how to go about doing that.
1) How should I be doing it?
2) In the template statement, what does the new do? I have seen
code with it and without it. Both seem to work, but which way is
correct/better?
***** Start of Code *****
<html>
<head>
<title>try4.html</title>
</head>
<body>
<script type="text/javascript">
function RealEstate
(
PropertyLocation,
PropertyValue
)
{
this.PropertyLocation=PropertyLocation;
this.PropertyValue=PropertyValue;
}
function Commercial
(
PropertyLocation,
PropertyValue,
PropertyZoning,
PropertyUse
)
{
this.PropertyLocation=PropertyLocation;
this.PropertyValue=PropertyValue;
this.PropertyZoning=PropertyZoning;
this.PropertyUse=PropertyUse;
}
Commercial.template=new RealEstate();
var Prop1=new Commercial("Kamloops",150000,"C-1","light commercial");
document.write("Location: "+Prop1.PropertyLocation+"<br>");
document.write("Value: "+Prop1.PropertyValue+"<br>");
document.write("Zoning: "+Prop1.PropertyZoning+"<br>");
document.write("Use: "+Prop1.PropertyUse+"<br>");
</script>
</body>
</html>
***** End of Code *****
Sincerely,
Gene Wirchenko
[toc] | [next] | [standalone]
| From | "Evertjan." <exjxw.hannivoort@interxnl.net> |
|---|---|
| Date | 2011-11-11 08:49 +0000 |
| Message-ID | <Xns9F9A63FF68208eejj99@194.109.133.133> |
| In reply to | #8225 |
Gene Wirchenko wrote on 11 nov 2011 in comp.lang.javascript: > The following code is meant is extend an object definition. The > code works. My question is whether it works best. Is the way that I > have initialised the properties in Commercial that are from RealEstate > the best way to do it? I have a feeling Perhaps I do not understand the Canadian "best way", but I strongly oppose these words in programming. Programming is not only a science but also an art, and it should be. Therefore there cann't be a "best" way to do it, as valueing art is subjective. Asking for a subjective "better" way to do something seems okay to me, as some ways seem quite silly to someone having found, by craft or by search, a "subjectively better" way. This NG is an ideal place to discuss the logic behind the is subjective "better way". Sometimes [or often?] even a weak or strong consensus may be formed, see the FAQ, but such consesnus does not mean "best". -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-11 05:40 -0800 |
| Message-ID | <5d71fb87-8e00-4c50-96db-4ad31589600b@u5g2000vbd.googlegroups.com> |
| In reply to | #8225 |
On Nov 10, 11:02 pm, Gene Wirchenko <ge...@ocis.net> wrote: > Dear JavaScripters: > < [snip] > If you are trying to accomplish inheritance - not entirely clear from the names of your objects and relevant code - please see http://javascript.crockford.com/prototypal.html In fact, please read all of Crockford, all of the FAQ and some other book on javascript and then repost. Thanks, Bob Or hell, just use $.extend() and go to the jQuery group. Yeah, I went there.
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-11 20:11 +0000 |
| Message-ID | <Ts$F80E5FYvOFwbw@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8231 |
On Fri, 11 Nov 2011 at 05:40:01, in comp.lang.javascript, beegee wrote: >On Nov 10, 11:02 pm, Gene Wirchenko <ge...@ocis.net> wrote: >> Dear JavaScripters: >> >< [snip] > > >If you are trying to accomplish inheritance - not entirely clear from >the names of your objects and relevant code - please see > >http://javascript.crockford.com/prototypal.html <snip> Or you could do inheritance the javascript way. John -- John Harris
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-11 21:02 -0800 |
| Message-ID | <cdvrb7d6ijld51jl1jd2hfksmf8vae95i4@4ax.com> |
| In reply to | #8242 |
On Fri, 11 Nov 2011 20:11:37 +0000, John G Harris
<john@nospam.demon.co.uk> wrote:
>On Fri, 11 Nov 2011 at 05:40:01, in comp.lang.javascript, beegee wrote:
>>On Nov 10, 11:02 pm, Gene Wirchenko <ge...@ocis.net> wrote:
>>> Dear JavaScripters:
>>>
>>< [snip] >
>>
>>If you are trying to accomplish inheritance - not entirely clear from
>>the names of your objects and relevant code - please see
>>
>>http://javascript.crockford.com/prototypal.html
> <snip>
>
>Or you could do inheritance the javascript way.
Which is?
(Remember me? I am the OP. I did have a question.)
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-12 16:46 +0000 |
| Message-ID | <YuSnExEkLqvOFw7y@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8249 |
On Fri, 11 Nov 2011 at 21:02:12, in comp.lang.javascript, Gene Wirchenko wrote: >On Fri, 11 Nov 2011 20:11:37 +0000, John G Harris ><john@nospam.demon.co.uk> wrote: > >>On Fri, 11 Nov 2011 at 05:40:01, in comp.lang.javascript, beegee wrote: >>>On Nov 10, 11:02 pm, Gene Wirchenko <ge...@ocis.net> wrote: >>>> Dear JavaScripters: >>>> >>>< [snip] > >>> >>>If you are trying to accomplish inheritance - not entirely clear from >>>the names of your objects and relevant code - please see >>> >>>http://javascript.crockford.com/prototypal.html >> <snip> >> >>Or you could do inheritance the javascript way. > > Which is? > > (Remember me? I am the OP. I did have a question.) One way to do it is shown in my web page at : <http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1> There are obviously several minor variations possible. For instance, you can make use of the 'call' function in constructors if you don't need to go too far back in time. John -- John Harris
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-14 12:26 -0800 |
| Message-ID | <96u2c7lqsl0sg7v34fd99a4294jqrojih7@4ax.com> |
| In reply to | #8260 |
On Sat, 12 Nov 2011 16:46:28 +0000, John G Harris
<john@nospam.demon.co.uk> wrote:
[snip]
>One way to do it is shown in my web page at :
>
> <http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1>
That appears to be about what I was thinking of. It was
confusing code for me. I was looking for a JavaScript keyword "base"
at first. Why is base a property rather than declared local with var?
Does it even matter?
>There are obviously several minor variations possible. For instance, you
>can make use of the 'call' function in constructors if you don't need to
>go too far back in time.
Thank you.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-15 06:49 -0800 |
| Message-ID | <3bd97554-1cac-4ff8-a42f-2c3925591d11@p9g2000vbb.googlegroups.com> |
| In reply to | #8342 |
On Nov 14, 3:26 pm, Gene Wirchenko <ge...@ocis.net> wrote: > That appears to be about what I was thinking of. It was > confusing code for me. I was looking for a JavaScript keyword "base" > at first. Why is base a property rather than declared local with var? > Does it even matter? There is no javascript keyword called 'base'. The rule to remember in Javascript is declaring a variable with 'var' makes it local in scope. Leaving off the var puts the variable into global scope. You should get into a habit of using 'var'. Even global variables should be var'd at the global level instead of implicitly for clarity. The reason for this is speed; the interpreter searches first at the local scope then moves up reaching the global scope last. Bob
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-15 09:47 -0800 |
| Message-ID | <9530731c-bd98-4d0b-99d2-b943b49d86fb@g21g2000yqc.googlegroups.com> |
| In reply to | #8351 |
On Nov 15, 9:49 am, beegee <bgul...@gmail.com> wrote: > On Nov 14, 3:26 pm, Gene Wirchenko <ge...@ocis.net> wrote: > The rule to remember in Javascript is declaring a variable with 'var' > makes it local in scope. Leaving off the var puts the variable into > global scope. You should get into a habit of using 'var'. Even > global variables should be var'd at the global level instead of > implicitly for clarity. The reason for this is speed; the > interpreter searches first at the local scope then moves up reaching > the global scope last. Uh, I need to correct this. Leaving off the var, puts the variable into the 'current' scope, I believe. This is not necessarily global. The advise I gave still stands. If you want the variable to be in the scope of your function object, use the 'this' prefix. Bob
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-15 15:43 +0000 |
| Message-ID | <GO8h8OImiowOFwDj@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8342 |
On Mon, 14 Nov 2011 at 12:26:05, in comp.lang.javascript, Gene Wirchenko
wrote:
>On Sat, 12 Nov 2011 16:46:28 +0000, John G Harris
><john@nospam.demon.co.uk> wrote:
>
>[snip]
>
>>One way to do it is shown in my web page at :
>>
>> <http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1>
>
> That appears to be about what I was thinking of. It was
>confusing code for me. I was looking for a JavaScript keyword "base"
>at first. Why is base a property rather than declared local with var?
>Does it even matter?
<snip>
It matters very much, and here's why :
Lets assume that we're constructing one of your Commercial objects. In
the constructor you need to create and set the extra properties that you
have in Commercial objects, PropertyZoning and PropertyUse. This is
straightforward :
this.PropertyZoning = PropertyZoning;
this.PropertyUse = PropertyUse;
But you also need to create and set the properties that you have in
RealEstate objects, PropertyLocation and PropertyValue. You could do it
directly in the same way, but this is repeating code that is already in
the RealEstate constructor.
Rewriting code is bad and should be avoided if possible. It is tedious,
even with copy and paste. It is error prone : you might get it wrong.
And one day you will want to change the base constructor so you have to
edit code all over the program to keep it all in step. (Will you get
that right?)
It's much better to run the RealEstate code to do the job for you. Can
you do this?
First, you need to be able run RealEstate as an ordinary function as we
don't want to build another new object. Can this be done? Yes it can!
You simply don't write 'new' in front of it. (Attention Mr Crockford).
Second, you need to call RealEstate with the new Commercial object as
the 'this' value. This is easy using RealEstate's call method :
RealEstate.call(this, [PropertyLocation, PropertyValue]);
// 'this' is the new Commercial object
However, back when I was writing my javascript note 'call' was new and
not in all popular browsers so an older clumsier way was needed.
Remember that if you do a.b() then the function b is called with the
'this' value set to a. So, we create a property of the Commercial object
under construction with the name 'base' (because that's what Netscape
called it and there isn't a really better name), and use it to hold the
function object RealEstate. Now it's easy :
this.base(PropertyLocation, PropertyValue);
// 'this' is the new Commercial object
After using it you delete the temporary property 'base' because it's no
use for any other job.
There you have it. No matter how complicated the base constructor is,
and no matter if the base itself is derived, it runs properly in the
derived constructor. Your only effort is to feed it the right arguments.
John
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-15 10:31 -0800 |
| Message-ID | <6qb5c75u67t4jg60v5ipc11qu6e4d5it0h@4ax.com> |
| In reply to | #8352 |
On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris
<john@nospam.demon.co.uk> wrote:
[snip]
>However, back when I was writing my javascript note 'call' was new and
>not in all popular browsers so an older clumsier way was needed.
>Remember that if you do a.b() then the function b is called with the
>'this' value set to a. So, we create a property of the Commercial object
>under construction with the name 'base' (because that's what Netscape
My question is why a property. Why not just
var base=...
>called it and there isn't a really better name), and use it to hold the
As there could be multiple levels, "base" is potentially
confusing. Visual FoxPro uses "parent".
>function object RealEstate. Now it's easy :
>
> this.base(PropertyLocation, PropertyValue);
> // 'this' is the new Commercial object
>
>After using it you delete the temporary property 'base' because it's no
>use for any other job.
But if you had declared it with var, you would not need to do
this.
What am I missing about base's definition?
>There you have it. No matter how complicated the base constructor is,
>and no matter if the base itself is derived, it runs properly in the
>derived constructor. Your only effort is to feed it the right arguments.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Jake Jarvis <pig_in_shoes@yahoo.com> |
|---|---|
| Date | 2011-11-15 23:35 +0100 |
| Message-ID | <9ig7q2Fj0tU1@mid.uni-berlin.de> |
| In reply to | #8356 |
On 15.11.2011 19:31, Gene Wirchenko wrote: > On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris > <john@nospam.demon.co.uk> wrote: > > [snip] > >> However, back when I was writing my javascript note 'call' was new and >> not in all popular browsers so an older clumsier way was needed. >> Remember that if you do a.b() then the function b is called with the >> 'this' value set to a. So, we create a property of the Commercial object >> under construction with the name 'base' (because that's what Netscape > > My question is why a property. Why not just > var base=... > >> called it and there isn't a really better name), and use it to hold the > > As there could be multiple levels, "base" is potentially > confusing. Visual FoxPro uses "parent". > >> function object RealEstate. Now it's easy : >> >> this.base(PropertyLocation, PropertyValue); >> // 'this' is the new Commercial object >> >> After using it you delete the temporary property 'base' because it's no >> use for any other job. > > But if you had declared it with var, you would not need to do > this. > > What am I missing about base's definition? <snip> The idea is to have 'this' inside the function you assigned to base refer to a certain value. -- Jake Jarvis
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-16 20:42 +0000 |
| Message-ID | <vIr8OUFMBCxOFwOA@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8356 |
On Tue, 15 Nov 2011 at 10:31:27, in comp.lang.javascript, Gene Wirchenko wrote: >On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris ><john@nospam.demon.co.uk> wrote: > >[snip] > >>However, back when I was writing my javascript note 'call' was new and >>not in all popular browsers so an older clumsier way was needed. >>Remember that if you do a.b() then the function b is called with the >>'this' value set to a. So, we create a property of the Commercial object >>under construction with the name 'base' (because that's what Netscape > > My question is why a property. Why not just > var base=... > >>called it and there isn't a really better name), and use it to hold the > > As there could be multiple levels, "base" is potentially >confusing. Visual FoxPro uses "parent". > >>function object RealEstate. Now it's easy : >> >> this.base(PropertyLocation, PropertyValue); >> // 'this' is the new Commercial object >> >>After using it you delete the temporary property 'base' because it's no >>use for any other job. > > But if you had declared it with var, you would not need to do >this. > > What am I missing about base's definition? <snip> To expand on what Jake said : You've done this.base = RealEstate; so both this.base and RealEstate refer to the same function object. When you call that function it does this.PropertyValue = PropertyValue; and suchlike during its execution. It's important here that 'this' is the new Commercial object under construction, not some other object such as the Global object. If base is declared as a local (or global) var object then the 'this' value will be wrong. On the other hand, if base is a property of the object under construction then the 'this' value will be the object under construction, as desired. Hence, base is a property of the new object, needed only temporarily for this specialised purpose. As for its name, it only lasts for 3 lines so the name is not all that important. As it takes a whole sentence, if not several, to say what it's for, I doubt if a good name is possible. E.g 'thisConveyer' says what it does, but not to where. Regardless, you can call it something else if you want to. I won't mind. John -- John Harris
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-12 09:35 -0800 |
| Message-ID | <ceef2dee-d472-4831-a10f-1e2cd39a0988@i6g2000vbe.googlegroups.com> |
| In reply to | #8242 |
On Nov 11, 3:11 pm, John G Harris <j...@nospam.demon.co.uk> wrote: > >http://javascript.crockford.com/prototypal.html > > <snip> > > Or you could do inheritance the javascript way. How is this not the javascript way? To me it looks exactly like the example you provided except you do not create a new intermediate object when you inherit. Bob
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-13 11:40 +0000 |
| Message-ID | <xETYMiIXy6vOFwcd@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8264 |
On Sat, 12 Nov 2011 at 09:35:36, in comp.lang.javascript, beegee wrote:
>On Nov 11, 3:11 pm, John G Harris <j...@nospam.demon.co.uk> wrote:
>
>> >http://javascript.crockford.com/prototypal.html
>>
>> <snip>
>>
>> Or you could do inheritance the javascript way.
>
>How is this not the javascript way?
>
>To me it looks exactly like the example you provided except you do not
>create a new intermediate object when you inherit.
I think you've misunderstood Crockford's way. There is no extra
intermediate object, but there are extra steps.
Doing it his way, first you call the following function, supplying your
desired prototype object, and hence prototype chain, as the parameter
'o' :
function object(o)
{
function F() {}
F.prototype = o;
return new F();
}
(The function might have a different name, or be a method of some
non-global object, depending on your preferences).
This gives you a new object with the right inheritance, but no
properties of its own. Now you have to add the desired own properties,
so :
var obj = object(protoCommercial);
obj.PropertyLocation = "Kamloops";
obj.PropertyValue = 150000;
obj.PropertyZoning = "C-1";
obj.PropertyUse = "light commercial";
This gives you a new Commercial object.
Incidentally, you make protoCommercial the same way from protoRealEstate
except you'll mostly be attaching methods rather than data properties.
Obviously, if you are going to make more than one Commercial object
you'll package all this code in a function. What are you going to call
it? You shouldn't call it Commercial else other people will think it's
a constructor. To follow the Java naming convention like so many do you
have to call it something like newCommercial .
So, to make a new Commercial object you end up doing
var c = newCommercial("Kamloops", 150000, "C-1", "light commercial");
Now what have you achieved? You've done exactly what a constructor would
have done but with two extra function calls. Was it worth it?
John
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-13 11:53 -0800 |
| Message-ID | <fa4cebe6-765e-4ea3-8eef-9a8ef2ade25b@y7g2000vbe.googlegroups.com> |
| In reply to | #8279 |
On Nov 13, 6:40 am, John G Harris <j...@nospam.demon.co.uk> wrote:
> I think you've misunderstood Crockford's way. There is no extra
> intermediate object, but there are extra steps.
>
Crockford's way:
function object(o)
{
1) function F() {}
F.prototype = o;
2) return new F();
}
Your way (and mine sometimes):
1) Commercial.prototype=new RealEstate("Kamloops",150000);
There, I counted em for you. function F() {} is the intermediate
object and the reason you want to do this is exactly so that the new
object does *not* inherit pre-existing property values.
Now in this case, the OP wants to inherit existing property values and
perhaps create a number of child objects. I'm really ambivalent about
the value of classical OO in Javascript. I've used it effectively
exactly once and if I had to do it over, would probably go a different
route. Once you start down this road, you start missing the other
aspects of class inheritance like super constructors and destructors,
function overloading, interfaces and protected,private members. Each
of these paradigms has to be emulated in JS and to what purpose?
Far better, in my opinion, to focus on data/ui separation and
performance.
bob
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-11-14 16:19 +0000 |
| Message-ID | <$FwoCrH69TwOFwUU@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #8291 |
On Sun, 13 Nov 2011 at 11:53:56, in comp.lang.javascript, beegee wrote:
>On Nov 13, 6:40 am, John G Harris <j...@nospam.demon.co.uk> wrote:
>
>> I think you've misunderstood Crockford's way. There is no extra
>> intermediate object, but there are extra steps.
>>
>
>Crockford's way:
>
>function object(o)
> {
>1) function F() {}
> F.prototype = o;
>2) return new F();
> }
>
>Your way (and mine sometimes):
>
>1) Commercial.prototype=new RealEstate("Kamloops",150000);
No, no, no, no, no, and yet again no!
A Commercial.prototype object and a RealEstate object have different
jobs to do. The Commercial.prototype object's job is to hold only
methods, except in peculiar circumstances. A RealEstate object's job is
to hold only data, except in rare circumstances. To repeat : they are
different kinds of object holding different kinds of properties.
>There, I counted em for you. function F() {} is the intermediate
>object and the reason you want to do this is exactly so that the new
>object does *not* inherit pre-existing property values.
a) There are two functions - object and F - and so two function objects.
Both are created at program start up. I don't see how either can be
called intermediate, but if one can then both are.
b) The whole point of inheritance is to inherit. You only want to avoid
inheriting if you've supplied the wrong prototype object. And the use of
the object function doesn't stop you picking up unwanted values from the
wrong kind of prototype object.
>Now in this case, the OP wants to inherit existing property values and
>perhaps create a number of child objects.
You don't mean "existing property values", do you? Inherited property
names, perhaps?
What on earth is a child object?
>I'm really ambivalent about
>the value of classical OO in Javascript. I've used it effectively
>exactly once and if I had to do it over, would probably go a different
>route. Once you start down this road, you start missing the other
>aspects of class inheritance like
>super constructors and
The Commercial constructor knows that its 'super constructor' is called
RealEstate, and doesn't want to call it as a constructor by the way.
>destructors,
Java doesn't have destructors, and some diehard Java enthusiasts insist
they aren't needed in spite of being given examples of dangling
resources.
ECMAScript assumes that garbage collection is sufficient, as does Java.
>function overloading,
Overloading assumes that different functions can have the same name
provided the parameters have different types.
ECMAScript parameters aren't typed, so you must use different function
names instead. (Or a type-detection mess in one catch-all function).
>interfaces
Why?
>and protected,private members.
Private properties are useful in million line programs. They stop
programmers deliberately corrupting the design.
It's not compulsory to corrupt the design.
>Each
>of these paradigms has to be emulated in JS and to what purpose?
You only emulate the features you need : the purpose being that a
particular feature is needed in your application.
>Far better, in my opinion, to focus on data/ui separation and
>performance.
Calling extra functions can't be good for performance.
Far better to focus on making your code maintainable in two years time
when the customer wants changes.
John
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | beegee <bgulian@gmail.com> |
|---|---|
| Date | 2011-11-15 10:02 -0800 |
| Message-ID | <d95a3631-82e4-4e89-baec-5d3c86d05c1c@o5g2000yqa.googlegroups.com> |
| In reply to | #8323 |
On Nov 14, 11:19 am, John G Harris <j...@nospam.demon.co.uk> wrote: > a) There are two functions - object and F - and so two function objects. > Both are created at program start up. I don't see how either can be > called intermediate, but if one can then both are. > > b) The whole point of inheritance is to inherit. You only want to avoid > inheriting if you've supplied the wrong prototype object. And the use of > the object function doesn't stop you picking up unwanted values from the > wrong kind of prototype object. > Okay. You are right, I think I did misunderstand Crockfords object() method. > You don't mean "existing property values", do you? Inherited property > names, perhaps? > > What on earth is a child object? No, I meant property values and a child object is one whose __PROTO__ member points to the parent object. I cannot, however, produce an example where the child inherits stale parent values. This is what I understood that Crockford's method avoids but I either misunderstood or the article I read was wrong. > >Each > >of these paradigms has to be emulated in JS and to what purpose? > > You only emulate the features you need : the purpose being that a > particular feature is needed in your application. Implement the incomplete set of OO functionality depending on the needs of the project? How is that even comprehendable to a programmer coming to an existing code base much less maintainable? > Calling extra functions can't be good for performance. It's often called factoring and it's very good for readability, extendability, and scalability. > Far better to focus on making your code maintainable in two years time > when the customer wants changes. Well, you and I will have to disagree on this if you mean adding OO. Focus on separation of UI and data will make the codebase agile (see above for the 'bility's).
[toc] | [prev] | [next] | [standalone]
| From | Elegie <elegie@anonymous.invalid> |
|---|---|
| Date | 2011-11-12 12:53 +0100 |
| Message-ID | <4ebe5e58$0$3173$426a74cc@news.free.fr> |
| In reply to | #8225 |
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");
---
Javascript inheritance is said to be "prototype-based". You create an
object, which is called a prototype, then you bind it to a constructor.
When the constructor is invoked (i.e. when the constructor function is
called using the "new" operator), then a new object is constructed, and
this object holds an implicit link towards the prototype. Later, when
resolving properties on this object, javascript first looks up
properties directly declared on the object, and if it finds nothing,
goes up the prototype chain until it finds something fitting (or
eventually fails when the chain has been consumed).
You appear to start walking on the path of javascript inheritance. I
therefore suggest that, before you go on, grab a copy of the
specification (I use the Third Edition and refer below to related
sections of this Edition, but you may try the Fifth Edition), and read
section 10 "Execution Contexts".
The specification can be found here:
<URL:http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm>
When you have read that section, try and study Douglas Crockford's
articles which have been pointed out by other posters.
> 2) In the template statement, what does the new do? I have seen
> code with it and without it. Both seem to work, but which way is
> correct/better?
Both are correct, but they should be used in different contexts.
Basically, using the "new" operator invokes the function as a
constructor and not as a function (i.e. it creates an object, sets its
prototype, and binds the "this" value used in the function's body to
that object).
See ECMAScript 262 Third Edition, section 13 "Function Definition".
Regards,
Elegie.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-12 16:04 +0000 |
| Message-ID | <4ebe98f1$0$28586$a8266bb1@newsreader.readnews.com> |
| In reply to | #8253 |
On Sat, 12 Nov 2011 12:53:57 +0100, 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, 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.
Rgds
Denis McMahon
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web