|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
FROM:Linux.COM
The Latest JavaScript Tuesday, 07 July 2009 06:59 Cameron Laird and Kathryn Soraiz
The latest JavaScript--1.7, 1.8 or 1.9, depending on how you think about it--is as fun to program as Python or Ruby. We've argued for years that JavaScript is an essential, respectable, and capable language; now it's even comfortable.
"Comfort"and "fun" are challenges to present objectively; for today, let's focuson a few language capabilities you might not recognize, and let youdecide how they ought to fit in your own programming.
Version Jumble Fora long time, our own JavaScript programming was conservative; wefocused on minimalism and careful use of libraries like jQuery. While JavaScript is a fine object-based language, and individual programmers have produced all sorts of esoteric prodigies with it, we never found as much characterin it as other languages provide: Tcl, Lua, Forth, Erlang, and manyothers. In (early) JavaScript, iteration through a list demanded a listindex! That certainly didn't feel like high-level programming.
Nowit's 2009 though, and we all have at least Firefox 3.0 available, orshould. Firefox 3.0 builds in 1.8 of JavaScript, which means that Arrayand String generics, iterators, closures, generators and generatorexpressions, comprehensions, strict interpretation, and more are allavailable for programming. 1.7 is not yet fully supported by the browsers other users might have, so judge soberly what your situation is before deciding on these features. There are plenty of reasons to like Firefox 3.0 and 3.5,though, and, if you know you can count on them, or are willing toinclude a compatibility library for other browsers, you ought to takeadvantage of modern JavaScript.
There are plenty of sources on the Web for detail on JavaScript, so we write on it only rarely. A few examples, though, will give "Regular Expressions" readers a glimpse of how current JavaScript looks:
Iterators, Closures, Lambdas, and More Formost of JavaScript's history, the simplest JavaScript collection datastructure has been the Array. You initialize and display a list of thelargest USA cities this way:
- var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
- for (i = 0; i < cityArray.length; i++) {
- document.writeln(cityArray);
- } It was possible to write
- var cityHash = []
- cityHash["first"] = 'New York City';
- cityHash["second"] = 'Los Angeles';
- cityHash["third"] = 'Chicago';
- cityHash["fourth"] = 'Houston';
- for (cityIndex in cityHash) {
- document.writeln(cityHash[cityIndex]);
- }
复制代码 Thisapproach doesn't always honor naive expectations, though, becausecityHash isn't a Hash or Associative Array as other languagesunderstand it. cityHash.length is zero. cityIndex in cityHash computes all cityHash's properties, so introduction of a framework or debugger might bring surprises.
JavaScript 1.6 gives the possibility to code with a new built-in forEach instead:
- var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
- cityArray.forEach(function(item, index) {
- document.writeln(item);
- });
复制代码 In this case, of course, document.writeln() is already a named function, so the expression can be abbreviated to
- var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
- cityArray.forEach(document.writeln);
复制代码 What'sinteresting, though, is the opportunity to inline anonymous actionsexactly at the point of their use by forEach or other relatively newJavaScript methods such as map, every, and filter.
JavaScript now has not only anonymous functions, but even closures.Quite a few languages have acquired closures over the last decade, andprogrammers are learning to make more stylish use of them. Here's anexample of use of a closure in JavaScript:
- function tell_accumulator() {
- var accumulator = 0;
- var fireAlert = function() {alert(accumulator);}
- accumulator++;
- return fireAlert;
- }
复制代码 Do you see what value tell_accumulator pops up the first time it's invoked?
Comprehensions are another powerful novelty for JavaScript. Just this week we needed code something like
- var active_sessions = [session_name(code) for each
- (code in actives) if budget(code) > 0]; In earlier environments, this would have required coding such as
- var active_sessions = [];
- active_counter = 0;
- for (i = 0; i < actives.length; i++) {
- if (budget(actives) > 0) {
- active_sessions[active_counter++] = session_name(actives);
- }
- }
复制代码 We like the maintainability the array comprehension brings the former definition.
Summary We repeat: there are many fine references to help learn JavaScript. JavaScript: The Good Parts,for example, is accurate, insightful, and just published this spring.We've only hinted at the advantages of recent JavaScript, let alone allthe other exciting ferment SVG, canvas, JSON, introspection, DOMpersistence, and so on bring modern browsers. Install Firefox 3.5, andlearn the new possibilities for yourself. Your JavaScript programminghorizons will expand more than you realize.
Kathryn This e-mail address is being protected from spambots. You need JavaScript enabled to view it and Cameron This e-mail address is being protected from spambots. You need JavaScript enabled to view it run their own consultancy, Phaseit, Inc.,specializing in high-reliability and high-performance applicationsmanaged by high-level languages. Cameron began coding JavaScript beforeit was called "JavaScript". Cameron and Kathryn write about scriptinglanguages and related topics in their "Regular Expressions" columns. |
|