开发者

GWT: Is it possible to start the project using GWT and then migrate eventually to a full javascript?

I am thinking of starting a web app using GWT because I am more comfortable with using Java than Javascript. Also, I want to know the web development best practices and how GWT optimizes the Javascript. I want to analyse the javascript output produced by GWT so that I can learn how to write good Javascript. Is it possible or is the javascript produced by GWT is illegible, to be understood only by the browser?

EDIT: Yes, I looked at *.nocache.js and it is not legible. Though I would continue with GWT for now,开发者_Go百科 I would like to know if there is any web-development framework that will help me set up a web app fast, but along the way guide me to best practices in Javascript? I prefer if my web-app is not tied too tightly with this framework and I can program in javascript (+jquery) if I want to.


Of course you can always create a tiny "case study" project in GWT, and let it compile with -style PRETTY or DETAILED, and then analyze

  • what GWT does for optimization and
  • what differences GWT creates for different browsers,

but I think this approach is limited to a micro detail level, and can hardly be applied successfully to a complete project.

However, you can learn a few nice concepts from GWT (actually more from reading the GWT documentation than from analyzing the generated code) and try to re-implement them for a pure JavaScript project:

  • How GWT automatically replaces CSS class names with obfuscated ones, so that you can use many small CSS files, and still guarantee uniqueness.
  • How GWT determines the browser first, and then loads the corresponding JS and CSS files only.
  • ...

Moreover, you could try writing a few good GWT apps, and find out, which concepts you use. Many of them will appear natural in Java, while they're not in JavaScript (this is my experience from creating a few JavaScript prototypes, and then rewriting them in GWT). You might find, that your Java code, as it grows, is easier to maintain than the same code written using a naive approach to JavaScript. When you think about it, some of the concepts can be transferred nicely to JavaScript, some of them not so much, and for some things, JavaScript has its own (dynamic) concepts, which wouldn't work well in Java. In any case, read/watch what Douglas Crockford has to say about JavaScript concepts.

Do not forget, that one of the reasons for the creation of GWT is, that it should make it possible to use Java typical architecture, patterns and idioms on the client side - which would be very hard to apply when writing code in JavaScript.


Your way of learning Javascript through analyzing GWT generated code seems to be a not so good way. I would suggest to focus on Javascript if you want to learn Javascript because compiled results always resemble to a schema, but if you do handwritten code, it would be definitely another result.

But using GWT is in my opinion a good way to have a look into the "web 2.0"-world


Although GWT can generate "readable" javascript using the -PRETTY option, it won't be really readable. The code will be difficult to understand and it would be structured in a way totally different compared to how you would do it with jQuery or any other javascript library. Testing and debugging will be almost impossible outside the java world.

On the other hand, you could access plain Javascript from GWT using JSNI, and viceversa using gwt-exporter.


You might find this as difficult as trying to learn assembler from the output of a compiled C program. Do you kids remember C? Assembler? Anyhoo, I would suggest you create a simple GWT program, equivalent to a "Hello world" sort of thing, and use the advice given here to get the pretty unobfuscated output. If you can understand the Javascript you're seeing and it bears a passing resemblance to what a human might have written using Javascript directly to accomplish the same task, then maybe this will work. If, on the other hand, what comes out is a huge amount of obscure initialization and setup, and a heavy reliance on external Javascript functions to perform even the most straightforward actions, then maybe learning Javascript the old-fashioned way will be a better approach.


I see it compressed by default, almost impossible to read. Maybe you have options to make it clear.

I think that if you use gwt you must go with it, because for instance when new browsers come you just need to compile the page again and it work perfectly.

If you want to write something in js go with jquery.


You can export a javascript api from your gwt, so you can interop with your gwt code from raw js or jquery.

http://code.google.com/p/gwt-exporter/

This will allow you to emit a cross-platform gwt library that will present your compiled api to raw javascript. Very useful for exporting your work in gwt to raw js projects.

It's also a good project to teach you both powerful java and javascript; it uses annotations and a custom generator to export java methods and fields.


Take a look at Google's Debugging and Compiling page. You can use -style PRETTY argument for the compiler to generate readable javascript.

Update :

GWT Compiler does many things when compiling your code from Java to Javascript. Ray Cromwell talks about this in Google I/O 2010 in his Optimizing Apps with GWT Compiler presentation. This is great use if you are interested in what GWT Compiler does to optimize javascript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜