Discussion:
[wdte-devel] About the JavaScript editor
Christopher Lenz
2004-02-27 19:09:16 UTC
Permalink
Howdy folks,

I've been investigating the JavaScript plugin today. I hope that the
outline page and classes view are fixed now, at least they're working
for me.

Anyway, I've been thinking about the structured presentation of
JavaScript in views such as the outline page, and I'm not sure the
current approach is really appropriate. JavaScript is being treated by
us as if it were a more declarative/static, less dynamic language than
it is. For example, for the outline we turn the following JavaScript
snippet:

function Point(x, y) {
this.x = x;
this.y = y;
}
new Point();
Point.prototype.toString = function() {
return "[Point " + x + ", " + y + "]";
}

into a "class" with the "instance variables" x and y, and the "class
method" toString.

This doesn't seem quite right to me: Point is a constructor. The
constructor adds properties to the object being constructed. (BTW, what
makes a constructor a constructor? just adding properties to 'this'?).
Point objects, like all objects in JavaScript, have a prototype, which
is the closest thing JavaScript has to a class. We add a function to
that prototype. We could also have done that in the constructor.

But we could also do:

if (debug) {
Point.prototype.toString = function() {
return "[Point " + x + ", " + y + "]";
}
}

Thus the method would only get "declared" if the debug variable
evaluated to true. And that's only the simplest variant of dynamic
stuff you can do in JavaScript.

I have no idea how such dynamism could be correctly outlined. As I
said, our current approach seems a bit cheesy to me, because we are
basically trying to fit JavaScript into the constraints of a static
language.

Does anyone have any insight to share here? Do you know tools that
provide sensible outlines for JavaScript?

Cheers,
Chris
--
Christopher Lenz
/=/ cmlenz at gmx.de
Jörg Schaible
2004-02-27 23:13:41 UTC
Permalink
Hi Chris,
Post by Christopher Lenz
Anyway, I've been thinking about the structured presentation of
JavaScript in views such as the outline page, and I'm not sure the
current approach is really appropriate. JavaScript is being treated by
us as if it were a more declarative/static, less dynamic language than
it is. For example, for the outline we turn the following JavaScript
function Point(x, y) {
this.x = x;
this.y = y;
}
new Point();
Point.prototype.toString = function() {
return "[Point " + x + ", " + y + "]";
}
into a "class" with the "instance variables" x and y, and the "class
method" toString.
This seems quite good to me :)
Post by Christopher Lenz
This doesn't seem quite right to me: Point is a constructor. The
constructor adds properties to the object being constructed. (BTW, what
makes a constructor a constructor? just adding properties to 'this'?).
Well, you cannot really tell. You may even build hybrids. Error is such a
beast defined in the spec. If you call it as function, it returns an
instance or you may call it as ctor.
Post by Christopher Lenz
Point objects, like all objects in JavaScript, have a prototype, which
is the closest thing JavaScript has to a class. We add a function to
that prototype. We could also have done that in the constructor.
Yes, but that will be done in every instance, while stuffing the prototype
it is done only once. And you may also add the variables to the prototype.
Post by Christopher Lenz
if (debug) {
Point.prototype.toString = function() {
return "[Point " + x + ", " + y + "]";
}
}
Thus the method would only get "declared" if the debug variable
evaluated to true. And that's only the simplest variant of dynamic
stuff you can do in JavaScript.
:)

But in this case I would add it to the outline, because you use it to locate
the method in the code. And remember, methods are often declared in such a
way too:

function Point_toString() { ... }
Point.prototype.toString = Point_toString;

And the function and the assignment to the prototype are not located
directly together. The difference: In this case you don't use an anonymous
function to defin the method, which makes a big difference in some
JavaScript debuggers, because you'll have a function name on the stack
instead of a lot of anonymous ... :)
Post by Christopher Lenz
I have no idea how such dynamism could be correctly outlined. As I
said, our current approach seems a bit cheesy to me, because we are
basically trying to fit JavaScript into the constraints of a static
language.
I have not seen the JavaScript plugin, but an outline based on the prototype
and anything that is assigned to this in the ctor seems quite reasonable.

And think of "statics" (vars or functions):

Point.PI = 3.14;
Post by Christopher Lenz
Does anyone have any insight to share here? Do you know tools that
provide sensible outlines for JavaScript?
I am not aware of a tool, but I have "javadoc" support for JavaScript
(unfortunately only as Perl script):
http://www.berlios.de/projects/jsunit

It is a link on the main page.

Regards,
Jörg
Alex Fitzpatrick
2004-02-28 04:49:44 UTC
Permalink
Post by Christopher Lenz
Howdy folks,
I've been investigating the JavaScript plugin today. I hope that the
outline page and classes view are fixed now, at least they're working
for me.
Thanks Chris, I'm just looking at your adaptor factory now, that's just
where I was heading. Trouble is I've been working so hard lately I'd
just sit in front of Eclipse for 5 mins in the evening and stare at the
code. I'm beginning to see the light at the end of the tunnel this week...

...
Post by Christopher Lenz
Does anyone have any insight to share here? Do you know tools that
provide sensible outlines for JavaScript?
The big problem with outlining JavaScript is the dynamic nature... You
have to be able to PREDICT what it's going to do.

I was surprised by how much you can accomplish with a little guesswork
and heuristics. I'm not certain that this is the right way to go either,
but it seems to work.
--
Alex
Alex Fitzpatrick
2004-02-28 05:09:02 UTC
Permalink
Post by Alex Fitzpatrick
Post by Christopher Lenz
Howdy folks,
I've been investigating the JavaScript plugin today. I hope that the
outline page and classes view are fixed now, at least they're working
for me.
Thanks Chris, I'm just looking at your adaptor factory now, that's
just where I was heading. Trouble is I've been working so hard lately
I'd just sit in front of Eclipse for 5 mins in the evening and stare
at the code. I'm beginning to see the light at the end of the tunnel
this week...
After more looking, it really is where I was heading! I just never had a
clear enought head to see it through. Very nice.
--
Alex
Christopher Lenz
2004-02-29 11:56:57 UTC
Permalink
Post by Alex Fitzpatrick
Post by Alex Fitzpatrick
Post by Christopher Lenz
I've been investigating the JavaScript plugin today. I hope that the
outline page and classes view are fixed now, at least they're
working for me.
Thanks Chris, I'm just looking at your adaptor factory now, that's
just where I was heading. Trouble is I've been working so hard lately
I'd just sit in front of Eclipse for 5 mins in the evening and stare
at the code. I'm beginning to see the light at the end of the tunnel
this week...
After more looking, it really is where I was heading! I just never had
a clear enought head to see it through. Very nice.
Yeah, I like the adapter approach, too. It's particularly useful
because we need visualization in both the outline and the "JavaScript
Classes" view (how about renaming that to "JavaScript Objects", BTW?)

I also very often have a hard time finding the right Eclipse API for a
particular task. This is especially true for the text/editor
frameworks. :-(

Cheers,
Chris
--
Christopher Lenz
/=/ cmlenz at gmx.de
Christopher Lenz
2004-02-29 12:17:06 UTC
Permalink
Post by Alex Fitzpatrick
Post by Christopher Lenz
Does anyone have any insight to share here? Do you know tools that
provide sensible outlines for JavaScript?
The big problem with outlining JavaScript is the dynamic nature... You
have to be able to PREDICT what it's going to do.
I was surprised by how much you can accomplish with a little guesswork
and heuristics. I'm not certain that this is the right way to go
either, but it seems to work.
I agree that it's working quite well. Maybe my problem is more with the
naming of elements: I'd prefer objects and prototypes instead of
classes and instances, properties instead of instance variables, etc.

The main problem for me is that my favorite way of coding a JavaScript
object is not supported:

function Point(x, y) { ... }
Point.prototype = {
moveBy = function(x, y) { ... },
toString = function() { ... }
};

Also, I think we should have an option to display inner functions in
the outline.

Cheers,
Chris
--
Christopher Lenz
/=/ cmlenz at gmx.de
Alex Fitzpatrick
2004-02-29 16:36:32 UTC
Permalink
Post by Christopher Lenz
Post by Alex Fitzpatrick
Post by Christopher Lenz
Does anyone have any insight to share here? Do you know tools that
provide sensible outlines for JavaScript?
The big problem with outlining JavaScript is the dynamic nature...
You have to be able to PREDICT what it's going to do.
I was surprised by how much you can accomplish with a little
guesswork and heuristics. I'm not certain that this is the right way
to go either, but it seems to work.
I agree that it's working quite well. Maybe my problem is more with
the naming of elements: I'd prefer objects and prototypes instead of
classes and instances, properties instead of instance variables, etc.
The main problem for me is that my favorite way of coding a JavaScript
function Point(x, y) { ... }
Point.prototype = {
moveBy = function(x, y) { ... },
toString = function() { ... }
};
Also, I think we should have an option to display inner functions in
the outline.
One of my testcases is to load up mozilla client code and see how well
it outlines. As a result both of your suggestions are both on the to-do
list. :-)
--
Alex
Christopher Lenz
2004-02-29 18:45:16 UTC
Permalink
Post by Alex Fitzpatrick
Post by Christopher Lenz
The main problem for me is that my favorite way of coding a
function Point(x, y) { ... }
Point.prototype = {
moveBy = function(x, y) { ... },
toString = function() { ... }
};
Also, I think we should have an option to display inner functions in
the outline.
One of my testcases is to load up mozilla client code and see how well
it outlines.
:-)
Post by Alex Fitzpatrick
As a result both of your suggestions are both on the to-do list. :-)
How about sharing that list via our issue tracker. That would help
others to participate in the development.

Cheers,
Chris
--
Christopher Lenz
/=/ cmlenz at gmx.de

Loading...