IE 의 array 의 splice 방법의 bug
1195 단어 JavaScript
var a=["a","b"];
a.splice(0); alert(a.length) // IE 에 서 는 2, 구 글 에 서 는 0.
정의 에 따 르 면 splice () 의 두 번 째 인 자 는 쓰 지 않 고 끝까지 삭제 하 는 것 입 니 다. 왜 그런 지 IE 는 하나 도 삭제 하지 않 는 다 고 생각 합 니 다.
조사 결과, 문 제 는 IE8 및 이하 버 전에 서 발생 하 였 으 며, 이 bug 를 복구 할 수 있 습 니 다. 인터넷 에서 제공 하 는 방안 은?
// check if it is IE and it's version is 8 or older
if (document.documentMode && document.documentMode < 9) {
// save original function of splice
var originalSplice = Array.prototype.splice;
// provide a new implementation
Array.prototype.splice = function() {
var arr = [], i = 0, max = arguments.length;
for (; i < max; i++){
arr.push(arguments[i]);
}
// if this function had only one argument
// compute 'deleteCount' and push it into arr
if (arr.length==1) {
arr.push(this.length - arr[0]);
}
// invoke original splice() with our new arguments array
return originalSplice.apply(this, arr);
};
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.