New in JavaScript(Array)

[b]New in JavaScript 1.8.1(Firefox3.5)[/b]

[url=https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object/GetPrototypeOf]Object.getPrototypeOf()[/url]
This new method returns the prototype of a specified object.

[url=https://developer.mozilla.org/En/Using_native_JSON]Using native JSON[/url]
Firefox 3.5 has native support for JSON.

New trim methods on the String object
The [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String]String [/url]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);
}
}


[b]New in JavaScript 1.6[/b]
[i]Array extras[/i]
There are seven new Array methods that can be separated into two categories, item location methods and iterative methods.
The item location methods are:
•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf]indexOf()[/url] - 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;
};
}

•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf]lastIndexOf()[/url] - 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:
•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every]every()[/url] - 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;
};
}

•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter]filter()[/url] - 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;
};
}

•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach]forEach()[/url] - 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);
}
};
}

•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map]map()[/url] - 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;
};
}

•[url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some]some()[/url] - 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 [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Introduced_in_JavaScript_1.6]Working with Arrays[/url]


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

•[url=https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce]reduce()[/url] - 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;
};
}

•[url=https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/reduceRight]reduceRight()[/url] - 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;
};
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值