By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

OO JavaScript? Who's using it?

P: n/a
Just by way of introduction, I'm currently the principal developer and
maintainer of the a JavaScript editor plug-in for Eclipse.

[https://sourceforge.net/projects/jseditor/ The plug-in as it stands
supports syntax highlighting and outlining of functions, classes and
their methods.]

When I took over the project in June it was for the purpose of adding
outlining facilities for JavaScript written in an OO style with classes
and so on. I have since discovered, to both my delight and horror, that
there are a variety of ways of accomplishing OO in JavaScript.

I was wondering if any of those people currently involved in large
JavaScript projects might be willing to provide me with some input on:

- the style they use
- what kind of editor support they would find useful

I look forward to your input. (And if you don't feel the newsgroup is
the right place for this, there are discussion forums on the website
maintainted by sourceforge...)

Cheers! [And season's greetings!]

--
Alex
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Alex Fitzpatrick" <al*********************@videotron.nospam.ca> wrote
in message news:6E********************@wagner.videotron.net.. .
<snip>
When I took over the project in June it was for the purpose
of adding outlining facilities for JavaScript written in an
OO style with classes and so on.
It has often been questioned in this group whether JavaScript has any
structures that can reasonably be labelled a "class", or whether any
terminology from class-based languages should be applied to JavaScript.

Personally I don't mind using "class" to describe a particular sub-set
of JavaScript object definitions, but only as a convenient label and
only if the corresponding code is employed in a way that does not render
the term inapplicable (Given the runtime mutability of both object
instances and constructor prototypes).
I have since discovered, to both my delight and horror,
that there are a variety of ways of accomplishing OO in
JavaScript.
And there is the problem, JavaScript is wonderfully flexible both in
terms of the structures that can be defined and the way in which they
can be employed.

There is also the question of what separates an object from a function
(as functions are function objects) and so: when would a code structure
that resulted in instances of function objects with public and possibly
(closure emulated) private members (and possibly also public static and
(closure emulated) private static members) qualify as equivalent to a
"class"? A question that might be best illustrated with code:-

var TimedQue = (function(){
var base, timer;
var interval = 50;
var newFncs = null;
function addFnc(next, f){
function t(){
next = next&&next();
if(f()){
return t;
}else{
f = null;
return next;
}
};
t.addItem = function(d){
if(next){
next.addItem(d);
}else{
next = d;
}
return this;
};
t.finalize = function(){
return ((next)&&(next = next.finalize())||(f = null));
};
return t;
};
function tmQue(fc){
if(newFncs){
newFncs = newFncs.addItem(addFnc(null, fc));
}else{
newFncs = addFnc(null, fc);
}
if(!timer){
timer = setTimeout(tmQue.act, interval);
}
};
tmQue.act = function(){
var fn = newFncs, strt = new Date().getTime();
if(fn){
newFncs = null;
if(base){
base.addItem(fn);
}else{
base = fn;
}
}
base = base&&base();
if(base||newFncs){
var t = interval - (new Date().getTime() - strt);
timer = setTimeout(tmQue.act, ((t > 0)?t:1));
}else{
timer = null;
};
};
tmQue.act.toString = function(){
return 'TimedQue.act()';
};
tmQue.finalize = function(){
timer = timer&&clearTimeout(timer);
base = base&&base.finalize();
newFncs = [];
};
return tmQue;
})();

TimedQue is a DHTML animation timer. The object assigned to the named
global variable is a function and it is employed by calling it with a
function reference as an argument. It adds the function to an internal
queue and then arranges that the function be repeatedly executed at
intervals so long as it returns true, dropping the function from the
queue as soon as its execution returns false (or type-converted
equivalent).

From one point of view TimedQue is just a function but it has two public
methods and might be said to have 5 private members. But a better
candidate for consideration is the function - t - returned by calls to -
addFnc - as it two has public methods and multiple instances of the
function object, defined with the code within the - addFnc - function,
are created. So does the code within the - addFnc - function qualify as
an inner "class" definition?

From the point of view of wanting to apply class-based terminology to
JavaScript TimedQue is an (the) instance of a singleton class with 2
public methods and 5 private members including a private factory method
that returns instances of the - t - inner class. It is going to be a
pain to try to pull that interpretation out from the source code, and
there is not one single instance of the JavaScript - Object - "class"
created or employed anywhere in the above code.
I was wondering if any of those people currently involved
in large JavaScript projects might be willing to provide
me with some input on:
Given the date, is anyone currently involved in anything? ;)
- the style they use
That is not an easy question to answer. The flexibility that JavaScript
offers means that tying yourself down to a ridged concept of "class" or
any one pattern of object definition/instantiation might be working
against the strengths of the language. I tend to be flexible and
consider what the code is going to do as the various styles all involve
some trade-off or another.
- what kind of editor support they would find useful


I am not sure, I tend to use textPad though it only recognises 6 keyword
classes when syntax highlighting and I would like probably 10 or so.

Richard.
Jul 20 '05 #2

P: n/a
Richard Cornford wrote:
"Alex Fitzpatrick" <al*********************@videotron.nospam.ca> wrote
in message news:6E********************@wagner.videotron.net.. .
<snip>
When I took over the project in June it was for the purpose
of adding outlining facilities for JavaScript written in an
OO style with classes and so on.

It has often been questioned in this group whether JavaScript has any
structures that can reasonably be labelled a "class", or whether any
terminology from class-based languages should be applied to JavaScript.

Personally I don't mind using "class" to describe a particular sub-set
of JavaScript object definitions, but only as a convenient label and
only if the corresponding code is employed in a way that does not render
the term inapplicable (Given the runtime mutability of both object
instances and constructor prototypes).


A very good point, but this would be very a confusing distinction for
many people, so I don't try to go there outside of an academic setting.

Perhaps what we need is a strong etymology of class-like constructs that
are possible in a prototype oriented language. Clearly class-like is
just one.
I have since discovered, to both my delight and horror,
that there are a variety of ways of accomplishing OO in
JavaScript.

And there is the problem, JavaScript is wonderfully flexible both in
terms of the structures that can be defined and the way in which they
can be employed.

There is also the question of what separates an object from a function
(as functions are function objects) and so: when would a code structure
that resulted in instances of function objects with public and possibly
(closure emulated) private members (and possibly also public static and
(closure emulated) private static members) qualify as equivalent to a
"class"? A question that might be best illustrated with code:-


-Example snipped for brevity- - nice example though-

This kind of syntax should be supportable, right down to the "inner"
class. JSEditor does not currently handle private members or inner
classes, but I'm hoping to add this soon.
From one point of view TimedQue is just a function but it has two public
methods and might be said to have 5 private members. But a better
candidate for consideration is the function - t - returned by calls to -
addFnc - as it two has public methods and multiple instances of the
function object, defined with the code within the - addFnc - function,
are created. So does the code within the - addFnc - function qualify as
an inner "class" definition?
Yup, and TimedQue is just implementing a Factory pattern! :-)
From the point of view of wanting to apply class-based terminology to
JavaScript TimedQue is an (the) instance of a singleton class with 2
public methods and 5 private members including a private factory method
that returns instances of the - t - inner class. It is going to be a
pain to try to pull that interpretation out from the source code, and
there is not one single instance of the JavaScript - Object - "class"
created or employed anywhere in the above code.


No problem at all. I just wish I could prove that today. Perhaps you've
just given me something to target for January.
I was wondering if any of those people currently involved
in large JavaScript projects might be willing to provide
me with some input on:


Given the date, is anyone currently involved in anything? ;)


Everyone's a comedian on UseNet. :-)
- the style they use

That is not an easy question to answer. The flexibility that JavaScript
offers means that tying yourself down to a ridged concept of "class" or
any one pattern of object definition/instantiation might be working
against the strengths of the language. I tend to be flexible and
consider what the code is going to do as the various styles all involve
some trade-off or another.


How about some more examples, that previous one was good.
- what kind of editor support they would find useful


I am not sure, I tend to use textPad though it only recognises 6 keyword
classes when syntax highlighting and I would like probably 10 or so.


What keyword classes would those be?

How about class browsing like Smalltalk or the Java Browsing Perspective
in Eclipse?

How many class-like behaviors do you maintain?

[I got started in this project because my employer was looking at a
project that would lead to hundreds of JavaScript classes. Doing this
with several developers meant that TextPad was not sufficient and
Eclipse offered no license fees and Perforce integration. (And some
future hope of UML integration.)]

Thanks for your input, it's nice to hear from others in the community.
[It was also nice to see some elegant code that I understood even though
I would have expressed it differently.]

--
Alex
Jul 20 '05 #3

P: n/a
Alex Fitzpatrick wrote:
Richard Cornford wrote:
Personally I don't mind using "class" to describe a particular sub-set
of JavaScript object definitions, but only as a convenient label and
only if the corresponding code is employed in a way that does not render
the term inapplicable (Given the runtime mutability of both object
instances and constructor prototypes).
A very good point, but this would be very a confusing distinction for
many people,


So these people should learn the language that they are using in *any* way.
so I don't try to go there outside of an academic setting.

Perhaps what we need is a strong etymology of class-like constructs that
are possible in a prototype oriented language. Clearly class-like is
just one.


Class-like is simply worng. Prototypes are fundamentally different
from classes since every object can be the prototype of another, and
OTOH two objects can have different properties even if they have
been created with the same constructor function. In prototype-based
languages like JavaScript 1.x, there is no strict class-binding that
imposes something else.
PointedEars, currently using JSEditor 0.3.3
Jul 20 '05 #4

P: n/a
Alex Fitzpatrick wrote:
Just by way of introduction, I'm currently the principal developer and
maintainer of the a JavaScript editor plug-in for Eclipse.
Thank you, and keep up the good work.
[https://sourceforge.net/projects/jseditor/ The plug-in as it stands
supports syntax highlighting and outlining of functions, classes and
their methods.]

When I took over the project in June it was for the purpose of adding
outlining facilities for JavaScript written in an OO style with classes
and so on. I have since discovered, to both my delight and horror, that
there are a variety of ways of accomplishing OO in JavaScript.
And there are no classes in JavaScript 1.x.
I was wondering if any of those people currently involved in large
JavaScript projects might be willing to provide me with some input on:

- the style they use
You need to be more specific. For now, read my postings here and in
de.comp.lang.javascript (if you can manage it), have a look at
http://pointedears.de.vu/scripts/ and feed the scripts to the plugin.
- what kind of editor support they would find useful


More and more reliable syntax highlighting, see my library
documentations (.htm) for examples (they use the style.css
file). With eclipse 3.0M4 and JSEditor 0.3.3, which I
understand for the latter is the latest release, the plugin
tends to forget where syntax highlighting should be applied.
Example: I often type

var foobar

where `var' is sometimes not hightlighted. When I then remove
the `v' and include it again, the keyword is finally highlighted.

Only constructor functions where the `this' special operator is
used are considered constructor functions by the plugin (then
falsely displayed as (C)lass in the Outline view). This is
obviously caused by the misguided attempt to impose class-based
terminology over a prototype-based language (see my other posting).
A function should be at least considered a constructor function if
it contains the `this' operator or is called with the `new' operator.
Example: math.js, where Exception is considered a "class" while
specialized exception functions are not, even if they are called
with the `new' operator in common functions.

When properties are unconditionally added to the prototype property
of an object, they should also appear as properties in the Outline
view.

A built-in JavaScript beautifier (and uglyfier ;-)) is needed.
Unlike the Java Editor's formatter, which was IIRC usable for
formatting JavaScript until eclipse 3.0M3, it needs to pay
attention to dynamic HTML, so that `<' within a string is not
considered an operator aso.

It would be nice if one could create a coder's documentation from
JavaScript source using special comments, like JavaDoc and PHPdoc
already do. Thus I have recently developed JavaScriptDoc[tm]:

http://pointedears.de.vu/scripts/JSDoc/

A working implementation of it in JavaScript (using quite cruel RegExp
parsing) is underway, but as the eclipse platform may already provide a
parser suitable for JavaScriptDoc, I would be glad if it could be part
of the JSEditor plugin once possible syntax inconsistencies have been
resolved.

These are my suggestions so far.

(Unlike your From header, headers of my postings conform to Internet
standards, and) I also welcome replies via electronic mail.
PointedEars
Jul 20 '05 #5

P: n/a
Thomas 'Ingrid' Lahn wrote:
PointedEars, currently using JSEditor 0.3.3


D'oh! It's 0.0.3, of course.

And I see that 0.0.4 is now available. D'oh again? ;-)
PointedEars, upgrading
Jul 20 '05 #6

P: n/a
Thomas 'Ingrid' Lahn wrote another monologue ;-)
Thomas 'Ingrid' Lahn wrote:
PointedEars, currently using JSEditor 0.3.3


D'oh! It's 0.0.3, of course.

And I see that 0.0.4 is now available. D'oh again? ;-)


JFYI: JSEditor 0.0.4 does not work with eclipse 3.0M4 (the preferences
page is missing and the editor cannot be associated.) So I stick to
0.0.3 until I have the opportunity to download the latest platform
milestone (> 70M) via DSL.
PointedEars
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.