Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34456 > unrolled thread
| Started by | bit-naughty@hotmail.com |
|---|---|
| First post | 2018-01-05 06:10 -0800 |
| Last post | 2018-01-05 15:14 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.javascript
Trying to understand OOP, Umpteenth time bit-naughty@hotmail.com - 2018-01-05 06:10 -0800
Re: Trying to understand OOP, Umpteenth time "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-01-05 15:48 +0100
Re: Trying to understand OOP, Umpteenth time John G Harris <niam@jghnorth.org.uk.invalid> - 2018-01-05 15:14 +0000
| From | bit-naughty@hotmail.com |
|---|---|
| Date | 2018-01-05 06:10 -0800 |
| Subject | Trying to understand OOP, Umpteenth time |
| Message-ID | <ff64c61e-75f0-43c3-b003-a6f2ef8ba3fc@googlegroups.com> |
OK, so I think I get it. If you have a class X (OK, you can't, but maybe by now you can...ugh! I donno, you get my point?), you can say var foo = new X() ; ,and then you can execute stuff *inside* X like this: foo.m() ;. But then you can also do var bar = new X();, and then you'd have a second instance of X, right? My question is WHY? Why would you need the same thing twice?? ie. in short, what is the whole POINT of doing OOP? Where would you NEED to do the var bar stuff, after you'd already done the var foo? Thanks for you guys' help, as always.
[toc] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2018-01-05 15:48 +0100 |
| Message-ID | <p2o37h$41l$1@solani.org> |
| In reply to | #34456 |
On 05.01.2018 at 15:10, bit-naughty@hotmail.com wrote: > OK, so I think I get it. If you have a class X (OK, you can't, but maybe by now you can...ugh! I donno, you get my point?), you can say var foo = new X() ; ,and then you can execute stuff *inside* X like this: foo.m() ;. > > But then you can also do var bar = new X();, and then you'd have a second instance of X, right? My question is WHY? Why would you need the same thing twice?? ie. in short, what is the whole POINT of doing OOP? Where would you NEED to do the var bar stuff, after you'd already done the var foo? new classname() creates a new object instance, what is quite comparable to a baking machine producing a bread. If there should be more than one bread, you have to press the button again. This again is quite similar to a car factory, which will produce many cars (not only a single one). If you want to shiftGear(), accelerate() or brake() a certain car, the others should not be affected, of course. -- Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2018-01-05 15:14 +0000 |
| Message-ID | <rf5v4dh92i7f27s855q6d3tgaf6pals0n2@4ax.com> |
| In reply to | #34456 |
On Fri, 5 Jan 2018 06:10:46 -0800 (PST), bit-naughty@hotmail.com wrote: >OK, so I think I get it. If you have a class X (OK, you can't, but maybe by now you can...ugh! I donno, you get my point?), you can say var foo = new X() ; ,and then you can execute stuff *inside* X like this: foo.m() ;. > >But then you can also do var bar = new X();, and then you'd have a second instance of X, right? My question is WHY? Why would you need the same thing twice?? ie. in short, what is the whole POINT of doing OOP? Where would you NEED to do the var bar stuff, after you'd already done the var foo? Example 1 In a simulation, you have a simulated car arriving at a road junction. And another simulated car arriving at a different time or from a different direction. And another, and another. Each simulated car is a different instance of Car, carrying details of its journey. After passing the junction each instance deposits statistics into a log then disappears into the sunset. Now run the simulation with different traffic light algorithms. Note that there can be several cars in the simulated road system at the same time. Example 2 You have a start date and a finish date. Each is an instance of Date. Example 3 You have several sets of data. Each is an instance of Set. Example 4 You want to have several complex numbers at the same time in your calculation. You could make each an instance of Complex, which you code yourself if you can't beg, borrow, or steal someone else's. Now you can do var z = x.add(y); // x & y are instances of Complex PS In C++ you can do z = x + y; but ECMAScript hasn't invented that facility, yet. John
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web