日期:2014-05-16  浏览次数:20330 次

JavaScript1.6新特性系列(翻译)

?

JavaScript 1.6 introduces several new features: E4X, several new?Array?methods, and Array and String generics.

----JavaScript 1.6 介绍了几个新的功能:E4X, 几个新的数组方法,以及Array和String泛型

?

?

?

Array extras(数组扩展)

?

There are seven new Array?methods that can be separated into two categories, item location methods and iterative methods.?

?

7个新的数组方法可用被分离成两大类:项的定位方法和迭代方法。

?

The item location methods are:

?

?

  • indexOf() -----returns the index of the given items's first occurrence.
  • lastIndexOf() ------returns the index of the given items's last occurrence.
项定位方法为:

  • indexOf() ------返回指定项首次出现的index
  • lastIndexOf() ------返回指定项最后一次出现的index

The iterative methods are :

  • every() -------runs a function on items in the array while that function is returning true.It returns true if the function returns true for every item it could visit.
  • filter() ? -------runs a function on every item in the array and returns an array of all items for which the function returns true.
  • forEach() ------runs a function on every item in the array.
  • map() ? ?------- runs a function on every item in the array and returns the results in an array
  • some() ?--------runs a function on items in the array while that function returns false.It returns true if the function returns true for any item it could visit.

迭代方法包括:

?

?

  • ?every() ----在数组中的每一个项运行一个函数当那个函数返回的是true。如方法可用遍历每一个项都返回的true,则返回true
  • filter() ? ?---- 在数组中的每一个项运行一个函数,并将这个函数返回true的项作为一个数组返回
  • forEach()--- 在数组中的每一个项运行一个函数
  • map() ------ 在数组中的每一个项运行一个函数,并将所有的结果作为数组返回
  • some()------在数组中的每一个项运行一个函数,如果这个方法访问的任何一个项返回true,则返回true

?

?

Array and String generics(数组和字符串泛型)

?

Sometimes you would like to apply array methods to strings.By doing this,you treat a string as an array of characters.For example,in order to check that every character in the variable str is a letter, you would write:

?

有的时候你会像在字符串上使用数组的一些方法。这样的话其实你就将字符串视为一个字符数组。例如:项要检查一个变量str中的每一个字符是不是字母,你可能会这样写:

?

?

?