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
- 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中的每一个字符是不是字母,你可能会这样写:
- function isLetter(character) {
- return (character >= "a" && character <= "z");
- }
- if (Array.prototype.every.call(str, isLetter))
- alert("The string '" + str + "' contains only letters!");
This notation is rather wasteful and JavaScript 1.6 introduces a generic shorthand:
这样的写法是有点浪费。JavaScript1.6介绍了一个泛型的简洁方式:
- if (Array.every(str, isLetter))
- alert("The string '" + str + "' contains only letters!");
- var num = 15;
- alert(String.replace(num, /5/, '2'));
后续会放出7个api的源码。。。。。
相关文档:
1. https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.6#Array_extras