Andrea Giammarchi씨가 크로스 브라우징을 지원하는 네이티브 array wrapper를 작성했습니다:
올해에 몇번이나 JavaScript Ninja들이 성능 손실이 없으면서도 강력한 메소드를 가진 라이브러리를 만들기 위해 네이티브 Array의 확장을 시도했었는지 모르겠습니다. 제가 마침내 Internet Explorer 8에서 크기 잠금을 제거하고 모든 다른 브라우저에서의 문제점을 해결할 방법을 찾아냈습니다.
그가 완성한 Stack 클래스입니다:
[code:js]
/**
* Choose a name for subclassed Array
*/
Stack = (function(){ // (C) Andrea Giammarchi - Mit Style License
/**
* Your personal Array constructor
*/
function Stack(length){
if(arguments.length === 1 && typeof length === "number")
this.length = -1 <length && length === length <<1>> 1 ? length : this.push(length);
else if(arguments.length)
this.push.apply(this, arguments);
};
// Solution 1:
// Declaration of generic function
// with an array as prototype
function Array(){};
Array.prototype = [];
// Solution 2:
// use the prototype chain to inherit
// Array constructor and its native prototype
Stack.prototype = new Array;
// Solution 3:
// overwrite inherited length with zero value
Stack.prototype.length = 0;
// Solution 4:
// redeclare toString method in this way
// to let JScript core feel better
Stack.prototype.toString = function(){
return this.slice(0).toString();
};
/**
* Return and assign subclassed Array
*/
Stack.prototype.constructor = Stack;
return Stack;
})();
/**
* Choose a name for subclassed Array
*/
Stack = (function(){ // (C) Andrea Giammarchi - Mit Style License
/**
* Your personal Array constructor
*/
function Stack(length){
if(arguments.length === 1 && typeof length === "number")
this.length = -1 <length && length === length <<1>> 1 ? length : this.push(length);
else if(arguments.length)
this.push.apply(this, arguments);
};
// Solution 1:
// Declaration of generic function
// with an array as prototype
function Array(){};
Array.prototype = [];
// Solution 2:
// use the prototype chain to inherit
// Array constructor and its native prototype
Stack.prototype = new Array;
// Solution 3:
// overwrite inherited length with zero value
Stack.prototype.length = 0;
// Solution 4:
// redeclare toString method in this way
// to let JScript core feel better
Stack.prototype.toString = function(){
return this.slice(0).toString();
};
/**
* Return and assign subclassed Array
*/
Stack.prototype.constructor = Stack;
return Stack;
})();
from Stack : A native Array wrapper that works on Ajaxian
'[IT] Web Tech' 카테고리의 다른 글
Firefox 3 RC 1을 위한 Firebug 1.2 beta (0) | 2008.05.31 |
---|---|
잘못 설정된 crossdomain.xml (0) | 2008.05.31 |
또 다른 JIT : JavaScript 정보 시각화 툴킷 (0) | 2008.05.30 |
Dojo와 Zend Framework의 통합본 릴리스 (0) | 2008.05.30 |
Stack : 네이티브 Array wrapper (0) | 2008.05.28 |
classy_inputs: 자동으로 클래스 이름을 추가하는 Rails 플러그인 (0) | 2008.05.28 |
더 쉽고 접근하기 편한 YouTube 플레이어 (0) | 2008.05.27 |
브라우저 쿠키 제약에 관한 연구 (0) | 2008.05.27 |
moo.rd 1.3 릴리스 (0) | 2008.05.26 |
댓글을 달아 주세요