IE 의 array 의 splice 방법의 bug

1195 단어 JavaScript
오늘 코드 를 쓰 고 보 니 IE 의 array 의 splice 방법의 bug
   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);
    	};
    }

좋은 웹페이지 즐겨찾기