gooner_47
Member
Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
|
OK, so I understand the concept. Fine with that. But all the examples in courses/tutorials are for things like "shapes" or "fruit".
Anyone got any real world examples of when you might use classes (in my case in client-side Javascript)?
Ta
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
jQuery uses objects.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
Not really because JavaScript technically isn't OO.. You can define classes but its not strictly OO.
Biology is a great example of OO, inheritance and abstract classes.
First of all you need to understand Types, a type defines an object and a type can be inherited and modified to create a new Type and therefore new objects.
You could say TAnimal is a type and TMonkey inherits TAnimal..
TMonkey would have its own properties that are only relevant to TMonkey and TAnimal would have properties that are relevant to all descendants.
So to make a monkey object we would do:
monkey = new TMonkey
But we can also pass TMonkey as TAnimal (because TAnimal is an ancestor of TMonkey).
function detectAnimal(animal: TAnimal)
{
if (animal is TMonkey) print('ITS A MONKEY');
else if (animal is TMouse) print('ITS A MOUSE');
else if (animal is TElephant) print('ITS A ELEPHANT;
else print('no fucking idea!');
}
Your objects can have infinite levels of inheritance.
TCat could inherit TAnimal but TDomesticCat could inherit TCat and so on..
I've probably just confused you even more now
[Edited on 08-08-2014 by DaveyLC]
|
gooner_47
Member
Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
|
Thanks. Nah I get that completely, I do understand inheritance.
My point was, when is a web application you're writing ever going to deal with animals and biology?
I was just looking for some more realistic scenarios for using inheritance in an average website.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
Its an example.. Your objects and classes will be subjective depending on what YOU want to achieve.
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
See this is why I hate OO
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
quote: Originally posted by gooner_47
I was just looking for some more realistic scenarios for using inheritance in an average website.
Well a 'user' object would be one example if you are using any sort of authentication.. then 'session'.
|
Laney
Member
Registered: 6th May 03
Location: Leeds
User status: Offline
|
Would something like this be a bit more useful?
code:
class User
def login
...
return "Login successful"
end
end
class AdminUser < User
...
def is_admin
return true
end
end
class ClientAdminUser < AdminUser
...
def can_edit
return false
end
end
class SuperAdminUser < AdminUser
...
def can_edit
return true
end
end
@client = ClientAdminUser.new
puts @client.login # "Login successful"
puts @client.can_edit? # false
@super_admin = SuperAdminUser.new
puts @super_admin.login # "Login successful"
puts @super_admin.can_edit? # true
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
I've never used it for web apps.
For programs where you are handling larger data sets it is useful to break down the information into manageable chunks.
For example, in the past I wrote an application to handle betting via betfair.
When looking at a particular match I would take the data they issue via their API and parse it into my own types to streamline data handled internally within the app.
For example, a type being a football match, a sub type being data about what has happened in the match etc.
I can't see the same need to handle large amounts of data in a web app (since it will be stored by name in a database)
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
If you're doing MVC style programming then you'd be doing some proper OOP. Using the biology example, you'd probably have an Animal model.
MVC in JS is pretty specialist stuff though, and a bit of a pain in the arse.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
quote: Originally posted by Laney
Would something like this be a bit more useful?
code:
class User
def login
...
return "Login successful"
end
end
class AdminUser < User
...
def is_admin
return true
end
end
class ClientAdminUser < AdminUser
...
def can_edit
return false
end
end
class SuperAdminUser < AdminUser
...
def can_edit
return true
end
end
@client = ClientAdminUser.new
puts @client.login # "Login successful"
puts @client.can_edit? # false
@super_admin = SuperAdminUser.new
puts @super_admin.login # "Login successful"
puts @super_admin.can_edit? # true
That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.
|
Laney
Member
Registered: 6th May 03
Location: Leeds
User status: Offline
|
quote: Originally posted by DaveyLC
That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.
I apologise.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
quote: Originally posted by Laney
quote: Originally posted by DaveyLC
That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.
I apologise.
Dont get me wrong I'm not trying to be an arse, its refreshing to see people actually considering objects these days so many programmers are just happy working with events and settings it makes baby Jesus cry.
|
gooner_47
Member
Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
|
quote: Originally posted by ed
If you're doing MVC style programming then you'd be doing some proper OOP. Using the biology example, you'd probably have an Animal model.
MVC in JS is pretty specialist stuff though, and a bit of a pain in the arse.
I'm just getting to this now... I've been following this "course":
http://javascriptissexy.com/how-to-learn-javascript-properly/
Down in Week 7 it says to learn Backbone.js. I haven't actually gone through that section of it yet, but just reading articles about that and other MVC frameworks, tricky concept for me to get my head around. I'm sure it'll become clearer as I actually work through examples.
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
MVC in JavaScript seems pretty complex - I've been trying to get something up and running use Ember and it seems way more complicated than any of the PHP stuff I do.
|
A2H GO
Member
Registered: 14th Sep 04
Location: Stoke
User status: Offline
|
I'm building a web app for the company I work for using Ionic and AngularJS at the moment. Never written Javascript before or used this platform but pretty straightforward tbf (ps. Javascript sucks )
[Edited on 13-08-2014 by A2H GO]
|
gooner_47
Member
Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
|
Why does it suck?
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
JavaScript sucks because its not really object or pointer based.. For example when using an 'event' function it literally passes the code of the function so everything else is out of scope.
BUT for what it does its brilliant, what else is so cross-platform?
[Edited on 14-08-2014 by DaveyLC]
|
Rob_Quads
Member
Registered: 29th Mar 01
Location: southampton
User status: Offline
|
Javascript bugs me because it doesn't have anything to do with Java lol
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
Other than having the same syntax?
|
A2H GO
Member
Registered: 14th Sep 04
Location: Stoke
User status: Offline
|
quote: Originally posted by gooner_47
Why does it suck?
It sucks because I like Objective-C so much.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
quote: Originally posted by A2H GO
quote: Originally posted by gooner_47
Why does it suck?
It sucks because I like Objective-C so much.
Odd-bod
|