[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Newbie trying to understand object oriented programming

bit-naughty

2/5/2016 2:55:00 PM

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.
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";
}

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.....??!!!

This doesn't sound right......? Where is my reasoning flawed? Where am I screwing up? This doesn't sound like "this" means *that one*.....?



Thanks for your help.
50 Answers

ram

2/5/2016 3:09:00 PM

0

bit-naughty@hotmail.com writes:
>"this" means *that one*, right?

In ECMAScript, the keyword »this« is a primary expression
that will have the value of a call of the internal operation
»ResolveThisBinding« in the environment of this »this«, see
section 8.3.1.

However, understanding the keyword »this« of ECMAScript is
quite independent of »understanding« object-oriented
programming in general.

>function vegetables() {
>this.colour="brown";
>}
>var potatoes = new vegetables() ;
>This doesn't sound right......?

IIRC, It is recommended to write constructor functions with
an /uppercase/ initial letter. So, »Vegetables« might be
better style. Possibly, the lowercase »v« gave you the
subliminal impression that something was not right.

4ndre4

2/5/2016 6:57:00 PM

0

On 05/02/2016 14:55, bit-naughty@hotmail.com wrote:

[...]
> OK, I've read about and tried to understand OOP for a long time now, but I don't think I really get it.
>
> "this" means *that one*, right? Like, it's a cookie cutter.....?

https://en.wikipedia.org/...(computer_programming)

--
4ndre4
"The use of COBOL cripples the mind; its teaching should, therefore, be
regarded as a criminal offense." (E. Dijkstra)
"Ora, questo "Delta11" non è nulla di più di uno scemo del villaggio" -
http://goo...

Tim Slattery

2/5/2016 7:07:00 PM

0

bit-naughty@hotmail.com wrote:

>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.....??!!!
>
>This doesn't sound right......? Where is my reasoning flawed?

Not all vegetables are brown, so why does your constructor make them
all brown? The constructor should initialize things that are common to
*all* vegetables. Individual instantiations should set properties that
are unique to themselves.

--
Tim Slattery
tim <at> risingdove <dot> com

Aleksandro

2/5/2016 8:35:00 PM

0

On 05/02/16 11:55, 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.

I think that trying to understand OOP with Javascript's prototype-based
method to make them is not a really good idea. I suggest you first learn
C++'s.

Danny

2/5/2016 11:01:00 PM

0

To bit-n...@hotmail.com

well, if you need to make a Class for the vegetables
and differentiate them while inheriting, it'd be along
the lines of



function vegetable(colour, family, season, farm_name, sellerID){
this.colour = colour;
this.family = family;
this.season = season;
this.farm_name = farm_name;
this.seller = seller;
this.onHarvest = function(s){
return (this.season.indexOf(s) > 0) ? 1 : 0;
}
}

var potatoes = new vegetable("yellow", "tuberous delicious", "spring", "two guys from italy", 1W2ER589U);
potatoes.onHarvest("autumn") // will return 0 or false

var tomatoes = new vegetable("red", "tomatosum succulentus", "spring-summer", "cow's barn", 4TEY89P);
tomatoes.onHarvest("spring") // will return 1 or true

Scott Sauyet

2/6/2016 3:10:00 AM

0

Aleksandro wrote:
> bit-naughty@hotmail.com wrote:

>> OK, I've read about and tried to understand OOP for a long time now,
>> but I don't think I really get it.

> I think that trying to understand OOP with Javascript's prototype-based
> method to make them is not a really good idea. I suggest you first learn
> C++'s.

I disagree. I would suggest that C++/Java/C# are among the *worst*
ways to learn OOP. Smalltalk would be better. Python or perhaps Ruby
might be acceptable, but more unusual OOP styles like Self or
Javascript would also teach a fair bit, as would a language that never
really claims to be object-oriented, but contains some of the best OOP
ideas: Erlang.

Of course, I'd suggest that learning functional programming is more
interesting still, and there's a whole herd of languages that support
FP. That list intersects with the OOP world in at least Javascript,
Scala, LISP (if LISP can be considered to match any of these
paradigms, when really it simply invented them and mixes and matches
them as it pleases :-) ), and, with the same caveats, Erlang. Many
other languages straddle the line a bit differently, falling mostly
into one camp, but with enough support for the other to be able to
claim some degree of membership.

But still, C++ to teach OOP? In 2016? I think there are many better
choices.

-- Scott

RobG

2/6/2016 7:19:00 AM

0

On 6/02/2016 00:55, 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.

Have you tried the MDN article on *this*?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Oper...

> This question just popped into my head while mulling the whole thing over:
>
> "this" means *that one*, right? Like, it's a cookie cutter.....?

No. Within a function, *this* is assigned the value of an object (in
non-strict mode), see the article referenced above. It is not a "cookie
cutter".

>
> Then what about this?:
>
> if I have:
>
> 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.....??!!!
>
> This doesn't sound right......?

When a function is called with the *new* keyword (i.e. as a constructor)
then a new instance of the constructor (or class in other languages) is
created and assigned to its *this* parameter. So *this* references an
object that is a new instance of *vegetables*.

You then create a *colour* property of this new instance and assign it
the value "brown".

The constructor then returns the instance with a brown property.


> Where is my reasoning flawed? Where am I screwing up? This doesn't sound like "this" means *that one*.....?

I don't see any reasoning at all, you expressed a feeling, so maybe your
emotions are flawed? ;-)


--
Rob

4ndre4

2/6/2016 8:33:00 AM

0

On 06/02/2016 03:10, Scott Sauyet wrote:

[...]
> I disagree. I would suggest that C++/Java/C# are among the *worst*
> ways to learn OOP.

I wouldn't suggest C++ either because, with the latest additions of
C++11 and C++14 (and the C++17 to come), the language has become much
more complex and can be confusing for a newsbie. Java and C# are good
choices to learn OOP though. The classic three tenets of OOP,
encapsulation, inheritance and polimorphism, are implemented by both
languages.

--
4ndre4
"The use of COBOL cripples the mind; its teaching should, therefore, be
regarded as a criminal offense." (E. Dijkstra)

ram

2/6/2016 11:24:00 AM

0

RobG <rgqld@iinet.net.au> writes:
>Within a function, *this* is assigned the value of an object

That seems to be true. It can be exploited to wrap a
value into an object!

|< function toObject(){ return this; }
|> undefined

|< typeof 2
|> "number"

|< typeof toObject.call( 2 )
|> "object"

>You then create a *colour* property of this new instance and assign it
>the value "brown".

BTW, ECMAScript uses the spelling »color« IIRC.

Joao Rodrigues

2/6/2016 1:56:00 PM

0

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_Java...


> 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-n...

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/C...

class Vegetables {
constructor(colour) {
this.colour = colour;
}
}

var potatoes = new Vegetables('brown');
console.log(potatoes.colour); // brown


--
Joao Rodrigues