FreeOZ论坛

标题: The Latest JavaScript [打印本页]

作者: coredump    时间: 8-7-2009 14:50
标题: The Latest JavaScript
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:
  1. var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
  2.       for (i = 0; i < cityArray.length; i++) {
  3.         document.writeln(cityArray);
  4.       } It was possible to write
  5. var cityHash = []
  6.       cityHash["first"] = 'New York City';
  7.       cityHash["second"] = 'Los Angeles';
  8.       cityHash["third"] = 'Chicago';
  9.       cityHash["fourth"] = 'Houston';
  10.       for (cityIndex in cityHash) {
  11.         document.writeln(cityHash[cityIndex]);
  12.       }
复制代码
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:
  1.       var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
  2.       cityArray.forEach(function(item, index) {
  3.                            document.writeln(item);
  4.                         });
复制代码
In this case, of course, document.writeln() is already     a named function, so the expression can be abbreviated to
  1.      var cityArray = ['New York City', 'Los Angeles', 'Chicago', 'Houston'];
  2.       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:
  1.      function tell_accumulator() {
  2.         var accumulator = 0;
  3.         var fireAlert = function() {alert(accumulator);}
  4.         accumulator++;
  5.         return fireAlert;
  6.       }
复制代码
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
  1.       var active_sessions = [session_name(code) for each
  2.                                     (code in actives) if budget(code) > 0];      In earlier environments, this would have required coding such as
  3.        var active_sessions = [];
  4.       active_counter = 0;
  5.       for (i = 0; i < actives.length; i++) {
  6.         if (budget(actives) > 0) {
  7.           active_sessions[active_counter++] = session_name(actives);
  8.         }
  9.       }
复制代码
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.

作者: yuba    时间: 8-7-2009 15:01
世界快大同了




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2