473,505 Members | 15,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

oop design

Hi,
I read a lot on how to make in Javascript programs in a OOP way. After
I download
many third-party library (like YUI, prototype ecc.) and I have seen
that almost
all the function (class) are designed like literal object notation and
not in a normal
way like I learned ... now I'm confusing
on how to design my classes... I came to a Java/C++ experience ...

It seems that with object literal notation we cannot make all the OOP
constructs
that we can make with normal Javacript class creation constructs... so
why is it so largely used?

Thanks

Sep 10 '07 #1
11 1319
On Sep 10, 7:45 pm, josh <xdevel1...@gmail.comwrote:
Hi,
I read a lot on how to make in Javascript programs in a OOP way. After
I download
many third-party library (like YUI, prototype ecc.) and I have seen
that almost
all the function (class) are designed like literal object notation and
not in a normal
way like I learned ... now I'm confusing
on how to design my classes... I came to a Java/C++ experience ...

It seems that with object literal notation we cannot make all the OOP
constructs
that we can make with normal Javacript class creation constructs... so
why is it so largely used?
This may help, particularly the last bit about Object-Oriented:

<URL: http://www.crockford.com/javascript/javascript.html >

--
Rob

Sep 10 '07 #2
On 10 Set, 11:45, josh <xdevel1...@gmail.comwrote:
Hi,
I read a lot on how to make in Javascript programs in a OOP way. After
I download
many third-party library (like YUI, prototype ecc.) and I have seen
that almost
all the function (class) are designed like literal object notation and
not in a normal
way like I learned ... now I'm confusing
on how to design my classes... I came to a Java/C++ experience ...

It seems that with object literal notation we cannot make all the OOP
constructs
that we can make with normal Javacript class creation constructs... so
why is it so largely used?

Thanks
I don't see RobG answer...

Sep 11 '07 #3
On Sep 11, 4:54 pm, josh <xdevel1...@gmail.comwrote:
On 10 Set, 11:45, josh <xdevel1...@gmail.comwrote:
Hi,
I read a lot on how to make in Javascript programs in a OOP way. After
I download
many third-party library (like YUI, prototype ecc.) and I have seen
that almost
all the function (class) are designed like literal object notation and
not in a normal
way like I learned ... now I'm confusing
on how to design my classes... I came to a Java/C++ experience ...
Javascript doesn't have classes, it has objects and a form of
inheritance via the prototype chain. It is explained in the link I
provided.

It seems that with object literal notation we cannot make all the OOP
constructs
that we can make with normal Javacript class creation constructs... so
why is it so largely used?
Because it is an efficient way to make objects. There are no classes
in javascript, though some libraries try to make things like there are
because their authors think that it's simpler to pretend javascript is
OO rather than trying to understand javascript.

e.g. Prototype.js has:

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

which (surprise surprise) is just an object literal with one method.

The link I provided includes other links to ways to make javascript
emulate some of the attributes of classes.
I don't see RobG answer...
Do you mean don't see as in don't get it, or don't see literally, as
in can't see it at all. Here is the link again:

<URL: http://www.crockford.com/javascript/javascript.html >
Sep 11 '07 #4
On 11 Set, 11:21, RobG <rg...@iinet.net.auwrote:
Because it is an efficient way to make objects. There are no classes
in javascript, though some libraries try to make things like there are
because their authors think that it's simpler to pretend javascript is
OO rather than trying to understand javascript.

e.g. Prototype.js has:

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

which (surprise surprise) is just an object literal with one method.
so do you think that in a complex program when we can use
incapsulation,
inheritance and polymorfism is better not to use Javascript like a
class way?
and so how we can to abstract these concepts if we wanted to do?
Do you mean don't see as in don't get it, or don't see literally, as
in can't see it at all. Here is the link again:
oh I didn't see it as I didn't get it
<URL:http://www.crockford.com/javascript/javascript.html>
thanks
Sep 11 '07 #5
On Sep 11, 7:48 pm, josh <xdevel1...@gmail.comwrote:
On 11 Set, 11:21, RobG <rg...@iinet.net.auwrote:
Because it is an efficient way to make objects. There are no classes
in javascript, though some libraries try to make things like there are
because their authors think that it's simpler to pretend javascript is
OO rather than trying to understand javascript.
e.g. Prototype.js has:
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
which (surprise surprise) is just an object literal with one method.

so do you think that in a complex program when we can use
incapsulation,
inheritance and polymorfism is better not to use Javascript like a
class way?
OO is a design philosophy, all those concepts can be implemented in
javascript using objects - Douglas Crockford's article shows how to
implement some of them, like private and privileged members. If you
watch his video series, you'll get clues about how to implement other
OO concepts.

But OO isn't the best way to do everything, in the same way that a
procedural design isn't the best way for everything. I think part of
the magic of javascript is that it easily goes between the two.

and so how we can to abstract these concepts if we wanted to do?
Search the archives for posts, e.g.:

<URL:
http://groups.google.com.au/group/co...6a67d74e2f5ea0
>

--
Rob

Sep 11 '07 #6
RobG a écrit :
(snip)
>
OO is a design philosophy, all those concepts can be implemented in
javascript using objects - Douglas Crockford's article shows how to
implement some of them, like private and privileged members. If you
watch his video series, you'll get clues about how to implement other
OO concepts.
And in a previous post:
There are no classes
in javascript, though some libraries try to make things like there are
because their authors think that it's simpler to pretend javascript is
OO rather than trying to understand javascript.
(hope I'm not starting a flame-war here... If so, please just ignore
this post)

I may misunderstand you, but it seems you're not considering javascript
as an OOPL because it's not class-based - which I find a bit surprising.
AFAIK, OO stands for "*object* oriented", not "class oriented", and
there's just *no* notion of 'class' in the base OO concepts (ie : 1/ an
object is defined by an id, a state and a behaviour, 2/ an OO program is
made of objects communicating thru messages). IOW, the fact that most
mainstream OOPLs are class-based doesn't make the prototype-based
approach less OO.

As a side note, since javascript functions are themselves objects (and
FWIW, when defined at the 'top-level', attributes of the 'global' object
- usually the browser's 'window' object), using functions in
javascript is not necessarily doing procedural programming (as a matter
of fact, moderns javascript frameworks are more and more on the
functional programming side).
<OP>
Now there's at least a couple points where I totally agree with RobG :
OO is not the one-size-fits-all solution, and it's better to understand
the language you're using instead of trying to write
<whatever-other-languagewith it.
</OP>

My 2 cents...
Sep 11 '07 #7
On Tue, 11 Sep 2007 15:34:09 +0200, Bruno Desthuilliers wrote:
RobG a écrit :
(snip)
I find the lack of multiple inheritance and the inability to overload
get, set, assign, etc. problematic. And then there is the iterating over
properties of the entire prototype chain which I consider as strange
feature.
Sep 13 '07 #8
Thomas M. Farrelly wrote:
On Tue, 11 Sep 2007 15:34:09 +0200, Bruno Desthuilliers wrote:
>RobG a écrit :
(snip)
(sic!)

What a very useful quote. NOT.
I find the lack of multiple inheritance
Multiple inheritance is possible with ECMAScript implementations.
and the inability to overload get, set, assign, etc. problematic.
JavaScript provides getters and setters. Other ECMAScript implementations
do not. What would `assign' and `etc.' be?
And then there is the iterating over properties of the entire prototype
chain which I consider as strange feature.
Only because you don't understand it.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Sep 13 '07 #9
On Thu, 13 Sep 2007 09:43:58 +0200, Thomas 'PointedEars' Lahn wrote:
JavaScript provides getters and setters. Other ECMAScript implementations
do not. What would `assign' and `etc.' be?
Example please.

You know what an assignment is. Etc. is such as compare, iterators, etc.
>And then there is the iterating over properties of the entire prototype
chain which I consider as strange feature.

Only because you don't understand it.
Please explain.
Sep 14 '07 #10
On Fri, 14 Sep 2007 12:52:34 +0200, Thomas 'PointedEars' Lahn wrote:

From
http://developer.mozilla.org/en/docs...rs_and_Setters
This is good stuff. Was not aware of that. ( tin-tags is my first js and
clientside web project )

Is it also possible to "catch" all property accesses? If so, then one could
easily implement multiple inheritance by making the prototype object look
through a list of prototype objects for dispatch.

o.prototype = {
// this is the list of prototypes
prototypes : new Array(),
// seach for propertyName in prototypes
_get_ : function( propertyName ) { ... },
// set property locally, of course
_set_ : function( propertyName, value ) { ... }
}

so

o.prototype[ somePropertyName ]

would return a property from an object in the o.prototype.prototypes array
and not from o.prototype. The notion of getters and setters in python lets
you do this.

If this isn't possible to do quite like this, is there a different way to
avoid cut'n'paste inheritance. I imagine a horrifyingly bad hack to do this
:-P
>>What would `assign' and `etc.' be?
You know what an assignment is.

I know that, but I could not make anything of your statement that it would
not be possible to overload _"assign"_ as you already mentioned "set", which
is the same thing.

Maybe you should elaborate on such general statements in the first place.
For example you want

a = 1 ;

to be equivalent to

a.value = 1 ;

or

a.setValue( 1 ) ;

Guess this is mostly useful in statically typed languages, but it would be
nice to modify this behaviour in js, for example to preserve type or apply
data invariants and so forth. Using "a.value" with the overloading of
getters and setter does the trick though or you would write

a = AType( 1 ) ;

Which works too. Just a thought.
>Etc. is such as compare, iterators, etc.

That is a recursive statement, so not of much use.
Recursion is useful ;-)
>
User-defined comparators are possible using Array objects since JavaScript
1.1 (NN 3.0/NES 2.0, 1996-08 CE), JScript 2.0 (IE/MSHTML 3.0, 1996),
ECMA-262 (Edition 1, 1997-06).
This is quite bad.
The algorithm could be wrapped into a method that is provided the first and
the second value as arguments. It would then create the array consisting of
those two elements, look for a comparator method and call Array::sort()
passing the method reference if such a method exists, and so on. However, I
would like the possibility to overload the comparison operators directly
instead, of course.
Yes, that would be much better.
Iterators can be defined in JavaScript since version 1.7 (Firefox 2):

http://developer.mozilla.org/en/docs..._1.7#Iterators

However, it is not that hard to define and use them in earlier versions and
other implementations without that language feature.
It is unclear to me whether this allows for modifying the behavior of
for-in. Is it possible to set the iterator of Object to always do the
hasOwnProperty test for example?
Sep 19 '07 #11
On Fri, 14 Sep 2007 12:52:34 +0200, Thomas 'PointedEars' Lahn wrote:

From
http://developer.mozilla.org/en/docs...rs_and_Setters
This is good stuff. Was not aware of that. ( tin-tags is my first js and
clientside web project )

Is it also possible to "catch" all property accesses? If so, then one could
easily implement multiple inheritance by making the prototype object look
through a list of prototype objects for dispatch.

o.prototype = {
// this is the list of prototypes
prototypes : new Array(),
// seach for propertyName in prototypes
_get_ : function( propertyName ) { ... },
// set property locally, of course
_set_ : function( propertyName, value ) { ... }
}

so

o.prototype[ somePropertyName ]

would return a property from an object in the o.prototype.prototypes array
and not from o.prototype. The notion of getters and setters in python lets
you do this.

If this isn't possible to do quite like this, is there a different way to
avoid cut'n'paste inheritance. I imagine a horrifyingly bad hack to do this
:-P
>>What would `assign' and `etc.' be?
You know what an assignment is.

I know that, but I could not make anything of your statement that it would
not be possible to overload _"assign"_ as you already mentioned "set", which
is the same thing.

Maybe you should elaborate on such general statements in the first place.
For example you want

a = 1 ;

to be equivalent to

a.value = 1 ;

or

a.setValue( 1 ) ;

Guess this is mostly useful in statically typed languages, but it would be
nice to modify this behaviour in js, for example to preserve type or apply
data invariants and so forth. Using "a.value" with the overloading of
getters and setter does the trick though or you would write

a = AType( 1 ) ;

Which works too. Just a thought.
>Etc. is such as compare, iterators, etc.

That is a recursive statement, so not of much use.
Recursion is useful ;-)
>
User-defined comparators are possible using Array objects since JavaScript
1.1 (NN 3.0/NES 2.0, 1996-08 CE), JScript 2.0 (IE/MSHTML 3.0, 1996),
ECMA-262 (Edition 1, 1997-06).
This is quite bad.
The algorithm could be wrapped into a method that is provided the first and
the second value as arguments. It would then create the array consisting of
those two elements, look for a comparator method and call Array::sort()
passing the method reference if such a method exists, and so on. However, I
would like the possibility to overload the comparison operators directly
instead, of course.
Yes, that would be much better.
Iterators can be defined in JavaScript since version 1.7 (Firefox 2):

http://developer.mozilla.org/en/docs..._1.7#Iterators

However, it is not that hard to define and use them in earlier versions and
other implementations without that language feature.
It is unclear to me whether this allows for modifying the behavior of
for-in. Is it possible to set the iterator of Object to always do the
hasOwnProperty test for example?
Sep 19 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2092
by: adb | last post by:
I came up with a replication configuration that is basically the result of all the restrictions of replication as well as the restrictions of allowable software on work PC's and I was curious if...
3
4111
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
0
1888
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
7
2972
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
2356
by: Paul Cheetham | last post by:
Hi, I have moved an application from VS2003 to VS2005, and I am now unable to view most of my forms in the designer. The majority of the forms in my project are derived from class PACForm,...
1
6319
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
2492
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
19
3134
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
10
3619
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
4
2446
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.