Internet Explorer에서 JavaScript 배열 함수 수정 (indexOf, forEach 등)
자세한으로 다른 , 그렇지 않으면 분명히 잘 알려진, (확실히 버전 7, 일부 경우에, 버전 8) 인터넷 익스플로러에 특히 중요한 기능을 구현하지 않는 Array
(예 forEach
, indexOf
등).
여기저기서 여러 가지 해결 방법이 있지만, 자체 구현에서 복사하여 붙여 넣거나 해킹하는 대신 적절한 표준 구현 집합을 사이트에 접고 싶습니다. 유망한 것처럼 보이는 js-methods를 찾았 지만 다른 라이브러리가 더 많이 권장되는지 여기에 게시한다고 생각했습니다. 몇 가지 기타 기준 :
- 라이브러리는 브라우저가 이미 구현 한 기능에 대해 작동하지 않아야합니다 (
js-methods
여기서는 잘 작동하는 것으로 보입니다). - LGPL 이 허용 되지만 GPL이 아닌 경우에 문의하십시오 .
많은 사람들이 MDC 폴백 구현을 사용합니다 (예 : indexOf 등 ). 모든 인수의 유형을 명시 적으로 확인하는 범위까지 일반적으로 엄격하게 표준을 준수합니다.
불행하게도 저자가이 코드를 사소하고 자유롭게 사용할 수있는 것으로 간주하는 것은 분명하지만,이를 서면으로 명시 적으로 허가 한 사람은없는 것 같습니다. 전체 위키는 CC Attribution-ShareAlike입니다. CC가 코드 용으로 설계되지는 않았지만 수용 가능한 라이센스 인 경우입니다.
js-methods는 일반적으로 괜찮아 보이지만 함수가 있어야하는 방식의 가장자리 주위에 표준을 준수하지는 않습니다 (예 : 정의되지 않은 목록 항목, 목록을 변경하는 함수). 또한 dodgy stripTags 및 불완전한 UTF-8 코덱과 같은 의심스러운 방법을 포함하여 다른 임의의 비표준 방법으로 가득 차 있습니다 ( unescape(encodeURIComponent)
트릭이 필요하면 약간 불필요합니다 ).
가치있는 것에 대해, 여기 내가 사용하는 것이 있습니다 (저는 저작권이 있다고 말할 수 있다면 공개 도메인으로 공개합니다). 함수가 아닌 콜백이나 정수가 아닌 인덱스를 전달하는 것과 같은 어리석은 짓을 시도하지는 않지만 표준 호환을 시도한다는 점에서 형식 감지를 시도하지 않기 때문에 MDC 버전보다 약간 짧습니다. (내가 놓친 것이 있으면 알려주세요. ;-))
'use strict';
// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
if (arguments.length<=1) {
return function() {
return that.apply(owner, arguments);
};
} else {
var args= Array.prototype.slice.call(arguments, 1);
return function() {
return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
};
}
};
}
// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
String.prototype.trim= function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}
// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(find, i /*opt*/) {
if (i===undefined) i= 0;
if (i<0) i+= this.length;
if (i<0) i= 0;
for (var n= this.length; i<n; i++)
if (i in this && this[i]===find)
return i;
return -1;
};
}
if (!('lastIndexOf' in Array.prototype)) {
Array.prototype.lastIndexOf= function(find, i /*opt*/) {
if (i===undefined) i= this.length-1;
if (i<0) i+= this.length;
if (i>this.length-1) i= this.length-1;
for (i++; i-->0;) /* i++ because from-argument is sadly inclusive */
if (i in this && this[i]===find)
return i;
return -1;
};
}
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach= function(action, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
action.call(that, this[i], i, this);
};
}
if (!('map' in Array.prototype)) {
Array.prototype.map= function(mapper, that /*opt*/) {
var other= new Array(this.length);
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
other[i]= mapper.call(that, this[i], i, this);
return other;
};
}
if (!('filter' in Array.prototype)) {
Array.prototype.filter= function(filter, that /*opt*/) {
var other= [], v;
for (var i=0, n= this.length; i<n; i++)
if (i in this && filter.call(that, v= this[i], i, this))
other.push(v);
return other;
};
}
if (!('every' in Array.prototype)) {
Array.prototype.every= function(tester, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this && !tester.call(that, this[i], i, this))
return false;
return true;
};
}
if (!('some' in Array.prototype)) {
Array.prototype.some= function(tester, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this && tester.call(that, this[i], i, this))
return true;
return false;
};
}
Other ECMA262-5 methods not implemented here include Array reduce
/reduceRight
, the JSON ones and the few new Object
methods that can be reliably implemented as JS functions.
Take a look at Underscore.js.
Kris Kowal has compiled a small library that acts as a shim for ECMAScript 5 functions that may be missing from the browser's implementation. Some of the functions have been revised numerous times by other people to be optimized for speed and to work around browser bugs. The functions are written to follow the specification as closely as possible.
es5-shim.js was released under the MIT license, the Array.prototype extensions are near the top and you can chop and remove any functions you don't need quite easily. I also suggest you minify the script as the comments make it much larger than it needs to be.
By 'not implement key functions' you actually means 'conforms to the ECMA 262 3'rd ed' right? :)
The methods you are referring to are part of the new 5'th edition - for browsers not supporting this you can use the following 'shim' that extends 3'rd into 5'th http://github.com/kriskowal/narwhal-lib/blob/narwhal-lib/lib/global-es5.js.
Those scripts don't work well in my tests. I create a file with the same functions based on MDN documents.
Too many problems areas are solved in Internet Explorer 8. See the code in egermano / ie-fix.js.
With the Underscore.js
var arr=['a','a1','b'] _.filter(arr, function(a){ return a.indexOf('a') > -1; })
'programing tip' 카테고리의 다른 글
LINQ To 엔터티는 마지막 방법을 인식하지 못합니다. (0) | 2020.06.23 |
---|---|
postgresql에서 여러 열을 삭제하는 방법 (0) | 2020.06.23 |
잡히지 않은 TypeError : 정의되지 않은 [중복]의 'msie'속성을 읽을 수 없습니다 (0) | 2020.06.23 |
Entity Framework 용 데이터베이스를 다시 만드는 방법은 무엇입니까? (0) | 2020.06.23 |
스프링 클래스 패스 접두사 차이 (0) | 2020.06.23 |