object oriented javascript?

A

Alex Weber

I was given an assignment today as part of an interview and I couldn't
finish it in the allocated time and I'm really curious as to whether
there is a good solution at all... I really just downright could not
figure out how to implement the proper OO stuff in javascript such as
inheritance and type checking, etc. I know that JS doesn't strictly
support inheritance but I also know that it can be achieved using
prototypes.

This is the question, can anyone shed some light please? Thanks!

Task
You are given a stack of boarding cards for various transportations
that will take you from a point A
to point B via several stops on the way. All of the boarding cards are
out of order and you don't
know where your journey starts, nor where it ends. Each boarding card
contains information about
seat assignment, and means of transportation (such as flight number,
bus number etc).
Provide an API that let's you sort this kind of list and present back
a description of how to complete
your journey. For instance the API should be able to take an unordered
set of boarding cards,
provided in a format defined by you, and produce this list:
• Take train 78A from Madrid to Barcelona. Sit in seat 45B.
• Take the airport bus from Barcelona to Gerona Airport. No seat
assignment.
• From Gerona Airport, take flight SK455 to Stockholm. Gate 45B,
seat 3A. Baggage drop at
ticket counter 344.
• From Stockholm, take flight SK22 to New York JFK. Gate 22, seat
7B. Baggage will we
automatically transferred from your last leg.
• You have arrived at your final destination.
The list should be defined in a format that's compatible with the
input format.
 
R

RobG

I was given an assignment today as part of an interview and I couldn't
finish it in the allocated time and I'm really curious as to whether
there is a good solution at all...  I really just downright could not
figure out how to implement the proper OO stuff in javascript such as
inheritance and type checking, etc.  I know that JS doesn't strictly
support inheritance

Yes, it does. Strictly.

but I also know that it can be achieved using
prototypes.

Javascript has prototype inheritance and can emulate other types of
inheritance.

This is the question, can anyone shed some light please?  Thanks!

      Task [..]

Its pretty straight forwards as far as theory, but I'm lost for the JS
code.  

Then provide your high-level design of the API (which can be
programming language independant) and someone might show you an
implementation in javascript.
 
B

Bruno Desthuilliers

Alex Weber a écrit :
I was given an assignment today as part of an interview and I couldn't
finish it in the allocated time and I'm really curious as to whether
there is a good solution at all... I really just downright could not
figure out how to implement the proper OO stuff in javascript such as
inheritance and type checking, etc.

"proper OO stuff", hu ???

Whoever assert that inheritance[1] and even worse "type checking" -
which I assume one must understand as "declarative static typing"[2] -
are (implied : 'required') "proper OO stuff" is brainwashed beyond
repair and ought to be shotdown before he do any more harm.

Javascript already has all the "proper OO stuff" : objects with id,
state and behaviour, communicating thru messages.


[1] a vastly overrated concept anyway - unless of course you're using a
stupidly typed so-called 'OO' language that manages to mess up
polymorphism with implementation. In a real message-passing system, the
set of messages an object understands is not (strictly) defined by it's
'type', so you don't need inheritance to allow polymorphic dispatch. In
which case inheritance is restricted to implementation inheritance,
which is just some special case of delegation.

[2] cf [1]
 
E

Evertjan.

Alex Weber wrote on 25 aug 2009 in comp.lang.javascript:
I was given an assignment today as part of an interview

Kind of newspeak perhaps?

In my book,
an interview is an interview,
and does not involve assignments.

You could however be assigned by your editor to do an interview.

Were you the interviewer or the interviewee?

Ah, never trust journalists anyway.

PS:
my editor is Editpadpro,
who trusts me implicitly and never assigns me to do anything,
more the opposite: she just loves to do my Javascript colouring.

;-)
 
G

Gregor Kofler

Alex Weber meinte:
I was given an assignment today as part of an interview

[snip]

Sounds more like "homework" to me.

What are your best efforts so far?

Gregor
 
J

John G Harris

Alex Weber a écrit :
I was given an assignment today as part of an interview and I couldn't
finish it in the allocated time and I'm really curious as to whether
there is a good solution at all... I really just downright could not
figure out how to implement the proper OO stuff in javascript such as
inheritance and type checking, etc.

"proper OO stuff", hu ???

Whoever assert that inheritance[1] and even worse "type checking" -
which I assume one must understand as "declarative static typing"[2] -
are (implied : 'required') "proper OO stuff" is brainwashed beyond
repair and ought to be shotdown before he do any more harm.

Javascript already has all the "proper OO stuff" : objects with id,
state and behaviour, communicating thru messages.


[1] a vastly overrated concept anyway - unless of course you're using a
stupidly typed so-called 'OO' language that manages to mess up
polymorphism with implementation. In a real message-passing system, the
set of messages an object understands is not (strictly) defined by it's
'type', so you don't need inheritance to allow polymorphic dispatch. In
which case inheritance is restricted to implementation inheritance,
which is just some special case of delegation.

Here we have someone who knows that Smalltalk is the One True Language,
and that Simula and its children (C++, Java, etc) are different and
therefore All Wrong.

John
 
E

Evertjan.

Malcolm Dew-Jones wrote on 25 aug 2009 in comp.lang.javascript:
Evertjan. wrote:
: Alex Weber wrote on 25 aug 2009 in comp.lang.javascript:

: > I was given an assignment today as part of an interview

: Kind of newspeak perhaps?

: In my book,
: an interview is an interview,
: and does not involve assignments.

If the job interviewer gives you a sample task that requires writing,
and says "please solve this, I'll be back in twenty minutes" - then
what would you call that?

I would not.
The word "assignment" seems appropriate enough to me.

What is a job interviewer?
If he/she interviews a job, do you need to be present?
If the interviewer is absent,
there would be no interview anyway
in my view.

Newspeak or local [American?] slang as I suggested?

PS:
You wrongly skipped my ";-)"
 
B

Bruno Desthuilliers

John G Harris a écrit :
Alex Weber a écrit :
I was given an assignment today as part of an interview and I couldn't
finish it in the allocated time and I'm really curious as to whether
there is a good solution at all... I really just downright could not
figure out how to implement the proper OO stuff in javascript such as
inheritance and type checking, etc.
"proper OO stuff", hu ???

Whoever assert that inheritance[1] and even worse "type checking" -
which I assume one must understand as "declarative static typing"[2] -
are (implied : 'required') "proper OO stuff" is brainwashed beyond
repair and ought to be shotdown before he do any more harm.

Javascript already has all the "proper OO stuff" : objects with id,
state and behaviour, communicating thru messages.


[1] a vastly overrated concept anyway - unless of course you're using a
stupidly typed so-called 'OO' language that manages to mess up
polymorphism with implementation. In a real message-passing system, the
set of messages an object understands is not (strictly) defined by it's
'type', so you don't need inheritance to allow polymorphic dispatch. In
which case inheritance is restricted to implementation inheritance,
which is just some special case of delegation.

Here we have someone who knows that Smalltalk is the One True Language,
and that Simula and its children (C++, Java, etc) are different and
therefore All Wrong.

Nope. Just someone that is somewhat tired of all this 'mainstream OO'
brainwashing.

The notions of 'class' and 'inheritance' have never been fundamental
concepts of OO - and let's not talk about static typechecking (not that
it's necessarily a bad thing by itself - it has pros and cons -, just
that it's a totally orthogonal problem).

The only commonly admitted concepts of OO are
1 - objects being defined by identity, state and behaviour
2 - objects sending messages to each others

and that's *all* there is to "proper OO stuff". Everything else (class
vs prototype, inheritance, whathever) is language design decision. So
let's not confuse language-specific stuff with fundamental concepts.

Well, ok, this being said, I admit I have a bias toward dynamic
languages... !-)
 
J

John G Harris

The only commonly admitted concepts of OO are

There are textbooks written by CS professors saying that a language is
only object *based* if inheritance is not an option. Your 'commonly' is
not all that common.

1 - objects being defined by identity, state and behaviour

Anything, even a humble integer variable, that occupies storage space
has an identity - a hardware address if nothing else - and a state.

As for behaviour, an integer has an empty behaviour, but so can a
user-defined object.

Incidentally, have you noticed that objects in both Java and javascript
have hardware addresses as their only identifiers.
2 - objects sending messages to each others
<snip>

I trust that 'sending messages' includes function calls.

John
 
A

Alex Weber

On Wed, 26 Aug 2009 at 12:23:22, in comp.lang.javascript, Bruno

Desthuilliers wrote:



There are textbooks written by CS professors saying that a language is
only object *based* if inheritance is not an option. Your 'commonly' is
not all that common.


Anything, even a humble integer variable, that occupies storage space
has an identity - a hardware address if nothing else - and a state.

As for behaviour, an integer has an empty behaviour, but so can a
user-defined object.

Incidentally, have you noticed that objects in both Java and javascript
have hardware addresses as their only identifiers.


  <snip>

I trust that 'sending messages' includes function calls.

  John

Wow thanks for that, all of you. Very helpful and insightful replies!
 
B

Bruno Desthuilliers

John G Harris a écrit :
There are textbooks written by CS professors saying that a language is
only object *based* if inheritance is not an option.

There are textbooks written by CS professors that should not bother
writing anything. I've once had a CS professor supposed to be a guru
wrt/ OO. Turned out she knew nothing else than Java, and believed that
CLOS multimethods belonged to functionnal programming.

wrt/ object-based vs object-oriented, I'd say that a language is only
object-based if _polymorphism_ - that is, the ability for different
objects (for a definition of 'different' that usually but not necessarly
imply being instances of different classes) to understand the same
message each in it's own specific way - is not an option.

Indeed, if you only know statically typed OOPLs that mixup inheritance
and polymorphic dispatch, you might think inheritance is then required !-)
Your 'commonly' is
not all that common.

Please provide any commonly accepted definitions / axiom about OO that
apply to *all* existing 'OO' languages ?
Anything, even a humble integer variable, that occupies storage space
has an identity - a hardware address if nothing else - and a state.

As for behaviour, an integer has an empty behaviour,

Oh, well. So you can't, like, add an integer to another, divide it by
another, etc ?
but so can a
user-defined object.

Incidentally, have you noticed that objects in both Java and javascript
have hardware addresses as their only identifiers.

So is it in CPython too. And what's the point, exactly ?
<snip>

I trust that 'sending messages' includes function calls.

FWIW, that's how CLOS works. Ho, and Python too - even if indirectly.

Now just one question : you do understand the difference between a
concept and one possible implementation of a concept, don't you ?
 
L

Lasse Reichstein Nielsen

John G Harris said:
There are textbooks written by CS professors saying that a language is
only object *based* if inheritance is not an option. Your 'commonly' is
not all that common.



Anything, even a humble integer variable, that occupies storage space
has an identity - a hardware address if nothing else - and a state.

The point is that an object is a *value* that has an identity.
Numbers are values that does not have an identity (there is no
distinguishing one 42 from another).
As for behaviour, an integer has an empty behaviour, but so can a
user-defined object.

The integer does not have identity.
A variable might have identity (and be identified by an identifier),
but it's not a value.
Incidentally, have you noticed that objects in both Java and javascript
have hardware addresses as their only identifiers.

That's an implementation detail. The identity isn't available in the
language itself, except to compare two objects for being the same.
<snip>

I trust that 'sending messages' includes function calls.

That's the typical way, yes.

/L
 
P

Pherdnut

The official definition according to me: "The ability to wrap data
containers and behavior into the same thingy." That's OO. Trying to
define based on what is and isn't an object is like defining
rectangles from squares. Seems a rather procedural approach to the
problem. :p
 
J

John G Harris

The official definition according to me: "The ability to wrap data
containers and behavior into the same thingy." That's OO. Trying to
define based on what is and isn't an object is like defining
rectangles from squares. Seems a rather procedural approach to the
problem. :p

Another description : An object is something that can jump up and down
and shout and scream and throw rocks.

John
 
P

Pherdnut

Another description : An object is something that can jump up and down
and shout and scream and throw rocks.

  John

Hmm... maybe you should have kept that thought encapsulated. What am I
supposed to inherit from that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top