Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29543 > unrolled thread
| Started by | bit-naughty@hotmail.com |
|---|---|
| First post | 2016-02-05 06:55 -0800 |
| Last post | 2016-02-06 11:56 -0200 |
| Articles | 6 on this page of 46 — 14 participants |
Back to article view | Back to comp.lang.javascript
Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-05 06:55 -0800
Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-05 18:56 +0000
Re: Newbie trying to understand object oriented programming Tim Slattery <tim@risingdove.com> - 2016-02-05 14:07 -0500
Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-05 17:34 -0300
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-05 19:10 -0800
Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 08:33 +0000
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 08:24 -0800
Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 18:48 +0000
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-06 12:28 -0800
Re: Newbie trying to understand object oriented programming 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-06 21:01 +0000
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-07 15:23 -0800
Re: Newbie trying to understand object oriented programming Aleksandro <aleksandro@gmx.com> - 2016-02-08 11:45 -0300
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-08 13:39 -0800
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-11 08:09 -0800
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-11 12:22 -0800
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-12 11:09 +0000
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-12 13:34 -0800
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-13 14:40 +0000
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 10:18 -0800
Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-14 21:27 +0100
Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-14 10:13 -0400
Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-05-14 16:47 +0100
Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:05 -0400
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-14 10:08 -0700
Re: Newbie trying to understand object oriented programming "J. Clarke" <j.clarke.873638@gmail.com> - 2016-05-15 07:02 -0400
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:11 -0700
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-15 17:03 +0100
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-15 13:20 -0700
Re: Newbie trying to understand object oriented programming Gene Wirchenko <genew@telus.net> - 2016-05-16 11:55 -0700
Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-16 21:39 +0200
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-17 07:30 -0700
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-05-17 15:34 +0100
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 17:50 -0800
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-14 11:15 +0000
Re: Newbie trying to understand object oriented programming "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-02-14 11:03 -0800
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-15 20:18 +0000
Re: Newbie trying to understand object oriented programming Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-13 16:54 -0800
Re: Newbie trying to understand object oriented programming Danny <dann90038@gmail.com> - 2016-02-05 15:00 -0800
Re: Newbie trying to understand object oriented programming RobG <rgqld@iinet.net.au> - 2016-02-06 17:18 +1000
Re: Newbie trying to understand object oriented programming Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-06 16:01 +0100
Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-07 04:47 -0800
Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-07 13:16 +0000
Re: Newbie trying to understand object oriented programming John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-07 17:29 +0000
Re: Newbie trying to understand object oriented programming bit-naughty@hotmail.com - 2016-02-08 08:53 -0800
Re: Newbie trying to understand object oriented programming Tim Streater <timstreater@greenbee.net> - 2016-02-08 17:33 +0000
Re: Newbie trying to understand object oriented programming Joao Rodrigues <jr@none.com> - 2016-02-06 11:56 -0200
Page 3 of 3 — ← Prev page 1 2 [3]
| From | bit-naughty@hotmail.com |
|---|---|
| Date | 2016-02-07 04:47 -0800 |
| Message-ID | <9ffad5b6-4de6-463c-b919-bd139402c94e@googlegroups.com> |
| In reply to | #29556 |
On Saturday, February 6, 2016 at 12:49:07 PM UTC+5:30, RobG wrote: > So *this* references an > object that is a new instance of *vegetables*. > Ahh - THIS was the sentence that taught me the most out of everything you guys said. OK, well, this is going to take some time to sink into my brain, I'm just going to have to sleep on it till I get it. WHY have they done all this? Like, why have things been laid out this way? The idea behind OOP is that - MANY programmers will be collaborating on ONE piece of software, right? Like "Vegetables" will be written by somebody else, and I'll just be instantiating it, and using its insides in my code, without caring about what it's inside it, right? First of all, how do I know that it'll be called "Vegetables" - do I have to chat with the guy who wrote it and ask him/her what he/she called it inside his/her code???!!! Is an easy way to learn this stuff to use *libraries*, where I will, have to call, code that someone else wrote, I mean, that's the whole point of a library, of course..... I find the statement "this.colour=colour" inside the function that someone said very confusing, can we just name the variables separately, like "this.hue=colour", it's still the same thing, "potatoes.hue" will then be "brown",right? How do I then get "tomatoes.hue" to be "red"? I know that you can get fat books on OOP, will I have to read those to understand all the answers to my questions? I hope not! :) (I even tried reading one once, didn't understand a word, it just put me to sleep! :) ) I DO intend to stick with Javscript, yes, mainly cause that's the only language available inside a browser, and I want to make websites (which ignores anything of what the language IS, of course - but I'm helpless :( ). Thanks to all you guys though :)
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2016-02-07 13:16 +0000 |
| Message-ID | <070220161316290875%timstreater@greenbee.net> |
| In reply to | #29573 |
In article <9ffad5b6-4de6-463c-b919-bd139402c94e@googlegroups.com>,
<bit-naughty@hotmail.com> wrote:
>On Saturday, February 6, 2016 at 12:49:07 PM UTC+5:30, RobG wrote:
>
>> So *this* references an
>> object that is a new instance of *vegetables*.
>>
>
>
>Ahh - THIS was the sentence that taught me the most out of everything you guys
>said.
>OK, well, this is going to take some time to sink into my brain, I'm just
>going to have to sleep on it till I get it.
>
>WHY have they done all this? Like, why have things been laid out this way? The
>idea behind OOP is that - MANY programmers will be collaborating on ONE piece
>of software, right? Like "Vegetables" will be written by somebody else, and
>I'll just be instantiating it, and using its insides in my code, without
>caring about what it's inside it, right? First of all, how do I know that
>it'll be called "Vegetables" - do I have to chat with the guy who wrote it and
>ask him/her what he/she called it inside his/her code???!!!
>Is an easy way to learn this stuff to use *libraries*, where I will, have to
>call, code that someone else wrote, I mean, that's the whole point of a
>library, of course.....
>
>I find the statement "this.colour=colour" inside the function that someone
>said very confusing, can we just name the variables separately, like
>"this.hue=colour", it's still the same thing, "potatoes.hue" will then be
>"brown",right? How do I then get "tomatoes.hue" to be "red"?
It will be brown only because you insisted on putting:
this.colour = "brown"
inside your object constructor. You should have put:
spuddy = new vegetable ("brown")
and for the constructor you have:
function vegetable (colour)
this.hue = colour
then you'll be doing:
tomahto = new vegetable ("red")
I though this had already been pointed out?
--
"Please stop telling us what you feel. Please stop telling us what your
intuition is. Your intuitive feelings are of no interest whatsoever,
and nor are mine. I don't give a bugger what you feel, or what I feel.
I want to know what the evidence shows." -- Richard Dawkins
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2016-02-07 17:29 +0000 |
| Message-ID | <djvebb1m6snmue6vo58jqu9r7o3rihq93k@4ax.com> |
| In reply to | #29573 |
On Sun, 7 Feb 2016 04:47:25 -0800 (PST), bit-naughty@hotmail.com wrote: <snip> >WHY have they done all this? Like, why have things been laid out this way? <snip> What follows might help you. It says where it all comes from. Question : What is an object? That's "object" as in "Object Oriented." Answer : An object is something that can jump up and down and scream and shout and hurl rocks. Better still, it's something that knows how to do those things without your help. If you want it to hurl a rock you just say "hurl!" and it does that. Of course, we are talking about software here so instead of saying "hurl!" we call the objects's "hurl" function. You can think of this function being inside the object if you wish. But suppose you have two rock-hurling objects : one at the front of your castle, the other at the back. Call them f and b. If f and b have separate copies of the function's code inside themselves then it's not nice. If you have thousands of rock-hurling objects it becomes very inefficient. This problem is solved by having one copy of the function that is shared by all the rock-hurling objects. The one copy is kept somewhere known to rock-hurling objects. The function belongs to them. To hurl a rock you need to say which rock-hurler should hurl and call its hurl function. In ECMAScript, and several other languages, to make object b hurl you do b.hurl(); (assume there are no parameters needed here). Now the hurl function is shared by all rock-hurling objects but it will probably need to access something inside the object. How does it know that it is object b? That's what "this" does. The compiler arranges for "this" to be the same object as b. If the hurl function needs to increment b's count property its code does this.count += 1; and so on. These rock-hurling objects must have come into existence somehow. The usual way is for somewhere in the program to do f = new RockHurler(); b = new RockHurler(); RockHurler is a constructor function. That is, its job is to help build a new object of a particular kind, a rock-hurler in this example. A constructor usually needs to give the new object properties with suitable values. As before, the compiler arranges for "this" to be the object that the function is to work on. Expect the code in the constructor to do this.count = 0; among other things. What has been said so far applies as much to C++ and Java as it does to ECMAScript. There are details that have not been discussed. This is where there tend to be differences between languages, especially in ECMAScript. John
[toc] | [prev] | [next] | [standalone]
| From | bit-naughty@hotmail.com |
|---|---|
| Date | 2016-02-08 08:53 -0800 |
| Message-ID | <a297cc47-5163-4ca7-b0f3-a1174cc72348@googlegroups.com> |
| In reply to | #29576 |
On Sunday, February 7, 2016 at 10:59:23 PM UTC+5:30, John Harris wrote: > Now the hurl function is shared by all rock-hurling objects AHH - THIS was the vital info that was of most importance in everything you said. Wow! Thanks for typing all that out, man, appreciate it :) But the question is, when writing a piece of software, how do you predict that there's *going to _be_ a lot of rock-hurling objects, all of whom will *need* to have the same "hurl" function inside them* ? How often does this actually occur? Could someone please also answer the stuff I asked above about "Vegetables", as in, multiple people working on the same code? ...and also the stuff about libraries? Thanks.
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2016-02-08 17:33 +0000 |
| Message-ID | <080220161733161611%timstreater@greenbee.net> |
| In reply to | #29580 |
In article <a297cc47-5163-4ca7-b0f3-a1174cc72348@googlegroups.com>, <bit-naughty@hotmail.com> wrote: >On Sunday, February 7, 2016 at 10:59:23 PM UTC+5:30, John Harris wrote: > >> Now the hurl function is shared by all rock-hurling objects > >AHH - THIS was the vital info that was of most importance in everything you >said. > >Wow! Thanks for typing all that out, man, appreciate it :) > >But the question is, when writing a piece of software, how do you predict that >there's *going to _be_ a lot of rock-hurling objects, all of whom will *need* >to have the same "hurl" function inside them* ? Well that'll be a matter for you. It'll show up in your design that this object and that one need the same *hurl* function. If OTOH the *hurl* function is different, you'll need another class. Note that you can still call it *hurl* in both classes if you want, if it's doing essentially the same thing but requires different code in the two cases. >How often does this actually occur? Any time you've got lots of the same object showing up in your app. Rows in a table, f'rinstance, are likely to be all the same. Or tables, if you've got a number of those. -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <jr@none.com> |
|---|---|
| Date | 2016-02-06 11:56 -0200 |
| Message-ID | <n94u26$5pp$1@gioia.aioe.org> |
| In reply to | #29543 |
bit-naughty@hotmail.com wrote:
> Hi, OK, I've read about and tried to understand OOP for a long time
> now, but I don't think I really get it.
In this case, I'd suggest reading this article in MDN:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript>
> This question just popped
> into my head while mulling the whole thing over:
>
> "this" means *that one*, right? Like, it's a cookie cutter.....?
>
> Then what about this?:
>
> if I have:
>
> function vegetables() { this.colour="brown"; }
There is a convention about constructor functions that their names
should start with an upper case letter. Then you should write:
function Vegetables() {
this.colour = "brown";
}
>
> and I do both a var potatoes = new vegetables() ;
>
> AND a var tomatoes = new vegetables();
>
>
> Then BOTH potatoes.colour will be "brown" *AND* tomatoes.colour will
> be "brown", right.....??!!!
Yes, because all instances of Vegetables will have the property 'colour'
with the same value ('brown').
You should have declared Vegetables() as:
function Vegetables(colour) {
this.colour = colour;
}
var tomato = new Vegetables('red');
console.log(tomato.colour); // red
var potato = new Vegetables('brown');
console.log(potato.colour); // brown
Douglas Crockford once wrote: "JavaScript is a prototypal language, but
it has a *new* operator that tries to make it look sort of like a
classical language. That tends to confuse programmers, leading to some
problematic programming patterns."
<http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/>
It would be simpler if you used:
var vegetables = {
tomatoes: {
colour: 'red'
},
potatoes: {
colour: 'brown'
}
};
It is even simpler if you need to add properties to vegetables:
vegetables.bananas = {colour: 'yellow'};
var tomato = vegetables.tomatoes;
console.log(tomato.colour); // red
var potato = vegetables.potatoes;
console.log(potato.colour); // brown
var banana = vegetables.bananas;
console.log(banana.colour); //yellow
ECMAScript 2015 (aka ES6) introduced "class", which is a syntactical
sugar over JavaScript's existing prototype-based inheritance. See:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes>
class Vegetables {
constructor(colour) {
this.colour = colour;
}
}
var potatoes = new Vegetables('brown');
console.log(potatoes.colour); // brown
--
Joao Rodrigues
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.javascript
csiph-web