Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ajax In Action (2006).pdf
Скачиваний:
63
Добавлен:
17.08.2013
Размер:
8.36 Mб
Скачать

JavaScript for object-oriented programmers

589

590APPENDIX B

JavaScript for object-oriented programmers

There are many routes into becoming a JavaScript programmer, ranging from graphic design to a serious programmer coming up from the business tiers.

This appendix won’t aim to teach you how to program in JavaScript—there are already many good books and articles to help you do that. What I intend to record here are a few core concepts that will help Java and C# programmers make the leap to JavaScript programming in a relatively painless way. (The same is true to a lesser extent of C++ programmers, but C++ inherits a lot of strange flexibility from C, so that JavaScript should prove less of a shock to the system.) If you are a serious enterprise programmer with a grounding in OO design principles, then your first approaches to JavaScript may be overly influenced by your experience with languages such as Java and C#, and you may find yourself fighting against the language rather than working with it. I certainly did, and I’ve based this on my own experience as a programmer and in mentoring others along the same route.

JavaScript can do a lot of clever things that Java and C# can’t. Some of these can help you to write better code, and some can only help you to shoot yourself in the foot more accurately! It’s worth knowing about both, either to make use of the techniques or to avoid doing them unwittingly. If you are coming to Ajax from a structured OO language such as Java or C++, then I hope that reading this appendix will help you as much as I think it would have helped me a few years back!

B.1 JavaScript is not Java

What’s in a name? In the case of Java and JavaScript, a lot of marketing and relatively little substance. JavaScript was renamed from “livescript” at the last minute by Netscape’s marketing department, and now the name has stuck. Contrary to popular perception, JavaScript is not a descendent of the C family of languages. It owes a lot more to functional languages such as Scheme and Self, and it has quite a lot in common with Python, too. Unfortunately, it’s been named after Java and syntactically styled to look like Java. In places, it will behave like Java, but in many places, it just plain won’t.

Table B.1 summarizes the key differences.

Table B.1 Key features of JavaScript and their implications

Feature

Implications

 

 

Variables are loosely typed.

Variables are just declared as variables, not as integers, strings, or

 

objects of a specific class. In JavaScript, it is legal to assign values

 

of different types to the same variable.

 

 

 

continued on next page

JavaScript is not Java

591

 

 

Table B.1 Key features of JavaScript and their implications (continued)

Feature

Implications

 

 

Code is dynamically interpreted.

At runtime, code is stored as text and interpreted into machine

 

instructions as the program runs, in contrast to precompiled lan-

 

guages such as Java, C, and C#. Users of your website can gener-

 

ally see the source code of your Ajax application. Furthermore, it

 

allows for the possibility of code being generated dynamically by

 

other code without resorting to special bytecode generators.

 

 

JavaScript functions are

A Java object’s methods are tied to the object that owns them and

first-class objects.

can be invoked only via that object. JavaScript functions can be

 

attached to objects so that they behave like methods, but they

 

can also be invoked in other contexts and/or reattached to other

 

objects at runtime.

 

 

JavaScript objects are

A Java, C++, or C# object has a defined type, with superclasses

prototype-based.

and virtual superclasses or interfaces. This strictly defines its func-

 

tionality. Any JavaScript object is just an object, which is just an

 

associative array in disguise. Prototypes can be used to emulate

 

Java-style types in JavaScript, but the similarity is only skin deep.

 

 

These differences allow the language to be used in different ways and open up the possibility of a number of weird tricks worthy of a seasoned Lisp hacker. If you’re a really clever, disciplined coder, you can take advantage of these tricks to do marvelous things, and you might even do so beyond a few hundred lines of code. If, on the other hand, you only think you’re really clever and disciplined, you can quickly end up flat on your face.

I’ve tried it a few times and come to the conclusion that keeping things simple is generally a good thing. If you’re working with a team, coding standards or guidelines should address these issues if the technical manager feels it is appropriate.

However, there is a second reason for knowing about these differences and tricks: the browser will use some of them internally, so understanding what is going on can save you much time and pain in debugging a badly behaved application. In particular, I’ve found it helpful to know where the code is not behaving like a Java object would, given that much of the apparent similarity is only apparent.

So read on, and find out what JavaScript objects really look like when the lights are out, how they are composed of member fields and functions, and what a JavaScript function is really capable of.