`
sohighthesky
  • 浏览: 34844 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

New in JavaScript(Array)

阅读更多
New in JavaScript 1.8.1(Firefox3.5)

Object.getPrototypeOf()
This new method returns the prototype of a specified object.

Using native JSON
Firefox 3.5 has native support for JSON.

New trim methods on the String object
The String object now has trim(), trimLeft(), and trimRight() methods.
//fastest trim http://lifesinger.org/blog/2010/01/fastest-javascript-trim/
if(!String.prototype.trim){
    String.prototype.trim=function() {
        var s=this,whitespace = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000',
        i = 0, j = s.length - 1;
        while (i < s.length && whitespace.indexOf(s.charAt(i)) != -1) i++;
        while (j > i && whitespace.indexOf(s.charAt(j)) != -1) j--;
        return s.substring(i, j + 1);
    }
}


New in JavaScript 1.6
Array extras
There are seven new Array methods that can be separated into two categories, item location methods and iterative methods.
The item location methods are:
indexOf() - returns the index of the given item's first occurrence.
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

lastIndexOf() - returns the index of the given item's last occurrence.
if (!Array.prototype.lastIndexOf)
{
  Array.prototype.lastIndexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]);
    if (isNaN(from))
    {
      from = len - 1;
    }
    else
    {
      from = (from < 0)
           ? Math.ceil(from)
           : Math.floor(from);
      if (from < 0)
        from += len;
      else if (from >= len)
        from = len - 1;
    }

    for (; from > -1; from--)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


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.
if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}

filter() - runs a function on every item in the array and returns an array of all items for which the function returns true.
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

forEach() - runs a function on every item in the array.
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

map() - runs a function on every item in the array and returns the results in an array.
if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}

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.
if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp*/)
  {
    var i = 0,
        len = this.length >>> 0;

    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (; i < len; i++)
    {
      if (i in this &&
          fun.call(thisp, this[i], i, this))
        return true;
    }

    return false;
  };
}

For more information see Working with Arrays


New in JavaScript 1.8
JavaScript 1.8 is part of Gecko 1.9 (which is incorporated into Firefox 3).
Array extras
There are two new iterative Array methods included in JavaScript 1.8, specifically:

reduce() - runs a function on every item in the array and collects the results from previous calls.
if (!Array.prototype.reduce)
{
  Array.prototype.reduce = function(fun /*, initial*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value and an empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = 0;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          var rv = this[i++];
          break;
        }

        // if array contains no values, no initial value to return
        if (++i >= len)
          throw new TypeError();
      }
      while (true);
    }

    for (; i < len; i++)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}

reduceRight() - runs a function on every item in the array and collects the results from previous calls, but in reverse.
if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fun /*, initial*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value, empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = len - 1;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          var rv = this[i--];
          break;
        }

        // if array contains no values, no initial value to return
        if (--i < 0)
          throw new TypeError();
      }
      while (true);
    }

    for (; i >= 0; i--)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}
分享到:
评论

相关推荐

    javascript模拟php函数in_array

    Array.prototype.in_array=function(e){ var r=new RegExp(this.S+e+this.S); return (r.test(this.S+this.join(this.S)+this.S)); }; 用法如下: var arr=new Array(["b",2,"a",4,"test"]); arr.in_array('...

    Professional JavaScript for Web Developers英文版

    JavaScript use with HTML to create dynamic webpages, language concepts including syntax and flow control statementsvariable handling given their loosely typed naturebuilt-in reference types such as ...

    Javascript实现Array和String互转换的方法

    本文实例讲述了Javascript实现Array和String互转换的方法。分享给大家供大家参考,具体如下: Array类可以如下定义: 复制代码 代码如下:var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 ...

    Professional JavaScript for Web Developers, 3rd Edition

    built-in reference types such as object and array object-oriented programing powerful aspects of function expressions Browser Object Model allowing interaction with the browser itself detecting the ...

    浅析JavaScript Array和string的转换(推荐)

    var aValues = new Array();  如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20);  ——————如下2种定义方式是一样的——–1——- var aColors = new Array(); aColors[0] = red; ...

    JavaScript Cookbook

    Work with JavaScript objects, such as String, Array, Number, and Math Use JavaScript with Scalable Vector Graphics (SVG) and the canvas element Store data in various ways, from the simple to the ...

    JavaScript语言参考手册

    这一章包含了 JavaScript 的核心对象 Array,Boolean,Date,Function,Math,Number,Object 和 String。这些对象同时在客户端和服务器端的 JavaScript 中使用。 Array 属性 方法 Boolean 属性 方法 Date 属性 方法...

    JavaScript 圣经第5版-Javascript编程宝典--黄金版 .rar

    The book includes all the great content included in the JavaScript Bible, 4th Edition, an international bestseller, plus over 400 pages of new material. The Gold Bible features essential new ...

    Learning.JavaScript.Add.Sparkle.and.Life.to.Your.Web.Pages.3rd.Edition.14

    If you’re a programmer new to JavaScript, or even a beginner with little or no programming experience, this latest edition of practical book offers complete, no-nonsense coverage of this essential ...

    【JavaScript源代码】JavaScript 语句之常用 for 循环详解.docx

     JavaScript中循环语句不少,for、for in、for of和forEach循环,今天对比Array、Object、Set(ES6)、Map(ES6)四种数据结构循环语句支持的情况及区别。 新建四种数据类型的测试数据 let arr = [1, 2, 3, 4, 5, 6];...

    JavaScript音频性能工具Lissajous.zip

    // load an array of three AudioBuffers called 'drums', play them in 8th notes and give them // the sequence drums[0], drums[2], drums[1], drums[2] d = new track() d.sample...

    JavaScript1.6数组新特性介绍以及JQuery的几个工具方法

    JavaScript 1.6 引入了几个新的Array 方法,具体的介绍见:New in JavaScript 1.6 。这些方法已经被写进了ECMA262 V5。现代浏览器(IE9/Firefox/Safari/Chrome/Opera)都已经支持,但IE6/7/8不支持。jquery的工具...

    javascript学习笔记.docx

    12) 创建一个数组可用 new Array() 。可以在构造函数参数中指定数组的前n个元素的值。数组的元素可以具有任意类型,甚至是另外一个数组。同一数组中可以存在不同类型的元素。数组的length是可读写的。这也是缩短一个...

    Simplifying [removed] Writing Modern JavaScript with ES5, ES6, and Beyond pdf

    Learn to write modern JavaScript not by memorizing a list of new syntax, but with practical examples of how syntax changes can make code more expressive. Starting from variable declarations that ...

    JavaScript中文参考手册

    这一章包含了 JavaScript 的核心对象 Array,Boolean,Date,Function,Math,Number,Object 和 String。这些对象同时在客户端和服务器端的 JavaScript 中使用。 Array 属性 方法 Boolean 属性 方法 Date ...

    javascript语言精粹(中英文版)

    Array Literals Section 6.2. Length Section 6.3. Delete Section 6.4. Enumeration Section 6.5. Confusion Section 6.6. Methods Section 6.7. Dimensions Chapter 7. Regular Expressions Section 7.1....

    JavaScript中join()方法的使用简介

    JavaScript数组join()方法加入数组的所有元素为一个字符串。 语法 array.join(separator); 下面是参数的详细信息: separator : 指定字符串分开数组的每个元素。...var arr = new Array("First","Second","Th

    107个常用javascript语句

    var my_array:Array = new Array(); my_array[0] = 1; my_array[1] = 2; my_array[2] = 3; my_array[3] = 4; my_array[4] = 5; trace(my_array.toString()); // Displays 1,2,3,4,5. 此示例输出 1、2、3、4、5 作为 ...

    linqjs:use linq and lambda in javascript on es6, can use linq function in an Object or an Array or a String value | 一个方便对数组、字典、树形数据进行操作、筛选等操作的工具库

    LinqJsSince 2.1.0, I rewrite all to use new features of ES6. The performance be better, memory used less and using deferred execution.对 JavaScript 中原生数组、对象进行扩展, 提供了一些对数据的操作方法...

    Javascript编译器Traceur.zip

    Traceur 是一个来自 Google 的 Javascript 编译器,通过它可以体验一些很新并且有趣的 Javascript 语言特性,这些多数是还没有被当前浏览器实现的 ECMAScript 标准或草案,比如:数组比较、类、模块、迭代器、方法...

Global site tag (gtag.js) - Google Analytics