JavaScript 5_javascript 기술의 새로운 Array 메소드 구현 소개

WBOY
풀어 주다: 2016-05-16 17:56:23
원래의
961명이 탐색했습니다.
复제대码 代码如下:

/*!
* jLip JavaScript Library v0.1
*
* Copyright 2012, Lip2up (lip2up@qq.com)
* 무료 사용, NO LICENSE
*/
(기능 () {
function Extension(target, props) {
for (var m in props) {
if (target[m] === undefine) target[m] = props[m]
}
}
var fns = { Every: 1, some: 2, forEach: 3, map: 4, filter: 5 },
reduceError = '초기값이 없는 빈 배열 줄이기' ;
function Each(fn, _this, kind) {
var len = this.length, ret = kind == fns.filter ? []
: kind == fns.map ? : 정의되지 않음,
find = kind == fns.some, i, v
for (i = 0; i < len; i ) {
if (this[i] !== 정의되지 않음) {
v = fn.call(_this, this[i], i, this);
스위치(종류) {
case fns.every:
case fns.some:
if (v === 찾기) return find;
break;
case fns.map:
ret[i] = v
break
if; (v === true) ret[ret.length] = this[i];
break
}
}
}
return kind >= fns.forEach ? !find;
}
function Reduce(fn, init, right) {
var len = this.length, i, prev, inc = right ? -1:1;
if (len == 0 && init === 정의되지 않음)
throw TypeError(reduceError);
for (i = 오른쪽 ? len - 1 : 0, prev = init;
prev === 정의되지 않음 && (오른쪽 ? i >= 0 : i i = inc) {
prev = this[i];
}
if (prev === 정의되지 않음 && i == (오른쪽 ? -1 : len))
throw TypeError(reduceError);
for (; (맞습니다 ? i >= 0 : i < len); i = inc) {
if (this[i] !== 정의되지 않음)
prev = fn(prev, this [i], 나, 이것);
}
이전 반환;
}
extend(Array.prototype, {
every: function(fn, _this) {
return Each.call(this, fn, _this, fns.every);
},
some: function(fn, _this) {
return Each.call(this, fn, _this, fns.some)
},
forEach: function(fn, _this) {
return Each.call(this, fn, _this, fns.forEach);
},
map: function(fn, _this) {
return Each.call(this, fn, _this, fns. map);
},
filter: function(fn, _this) {
return Each.call(this, fn, _this, fns.filter)
},
reduce: function (fn, init) {
return Reduce.call(this, fn, init)
},
reduceRight: function(fn, init) {
return Reduce.call(this, fn, 초기화, 참)
}
});
})();

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!