2 * ビルトインオブジェクトに拡張したい10のメソッド
\r
3 * http://d.hatena.ne.jp/ofk/20080922/1222047483
\r
6 Function.prototype.apply || (Function.prototype.apply = function (x, y) {
\r
11 // apply 内で apply を呼んだ場合に備える
\r
15 if( x.constructor && x.constructor.prototype.__apply ){
\r
16 delete x.constructor.prototype.__apply;
\r
18 if( x.__apply ) delete x.__apply;
\r
22 if (!x.__apply) x.constructor.prototype.__apply = this;
\r
25 case 0: r = x.__apply(); break;
\r
26 case 1: r = x.__apply(y[0]); break;
\r
27 case 2: r = x.__apply(y[0], y[1]); break;
\r
28 case 3: r = x.__apply(y[0], y[1], y[2]); break;
\r
29 case 4: r = x.__apply(y[0], y[1], y[2], y[3]); break;
\r
30 case 5: r = x.__apply(y[0], y[1], y[2], y[3], y[4]); break;
\r
31 case 6: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5]); break;
\r
32 case 7: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6]); break;
\r
33 case 8: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7]); break;
\r
34 case 9: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8]); break;
\r
37 for (i = 0; i < j; ++i)
\r
38 a[i] = 'y[' + i + ']';
\r
39 //r = eval('x.__apply(' + a.join(',') + ')');
\r
40 // closuer compiler 対策
\r
41 r = (new Function( 'x,y', 'return x.__apply(' + a.join(',') + ')' ))( x, y );
\r
48 if( x.constructor && x.constructor.prototype.__apply ){
\r
49 delete x.constructor.prototype.__apply;
\r
51 if( x.__apply ) delete x.__apply;
\r
55 Function.prototype.call || (Function.prototype.call = function () {
\r
56 var a = arguments, x = a[0], y = [], i = 1, j;
\r
57 for (j = a.length; i < j; ++i)
\r
59 return this.apply(x, y);
\r
62 Array.prototype.pop || (Array.prototype.pop = function () {
\r
63 var r = this[this.length - 1];
\r
67 Array.prototype.push || (Array.prototype.push = function () {
\r
68 var a = arguments, i = 0, j = a.length, l = this.length;
\r
73 Array.prototype.shift || (Array.prototype.shift = function () {
\r
74 var r = this[0], i = 1, j = this.length;
\r
76 this[i - 1] = this[i];
\r
80 Array.prototype.unshift || (Array.prototype.unshift = function () {
\r
81 var a = arguments, l = a.length, j = this.length += l - 1;
\r
82 for (var i = j; i >= l; --i)
\r
83 this[i] = this[i - l];
\r
84 for (var i = 0; i < l; ++i)
\r
88 Array.prototype.splice || (Array.prototype.splice = function (x, y) {
\r
89 var a = arguments, s = a.length - 2 - y, r = this.slice(x, x + y);
\r
91 for (var i = this.length - 1, j = x + y; i >= j; --i)
\r
92 this[i + s] = this[i];
\r
95 for (var i = x + y, j = this.length; i < j; ++i)
\r
96 this[i + s] = this[i];
\r
99 for (var i = 2, j = a.length; i < j; ++i)
\r
100 this[i - 2 + x] = a[i];
\r
106 * JavaScript 1.6, Array.indexOfを下位互換実装する
\r
107 * http://www.inazumatv.com/contents/archives/7965
\r
110 Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, fromIndex ){
\r
111 var l = this.length >>> 0,
\r
113 if( l === 0 ) return -1;
\r
116 i = fromIndex || 0;
\r
117 i = i === -Infinity ? 0 : Math.floor( i < 0 ? -i : i );
\r
118 if( l <= i ) return -1;
\r
121 for( i = 0 <= i ? i : 0 < l + i ? l + i : 0; i < l; ++i ){
\r
122 if( this[ i ] === searchElement ) return i;
\r
130 * by http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html
\r
133 /* 正規表現が使われているため、まだ投入しない itozyun
\r
134 window.encodeURI || (window.encodeURI = function (x) {
\r
135 return ("" + x).replace(/[^!#$&-;=?-Z_a-z~]/g, function (s) {
\r
136 var c = s.charCodeAt(0), p = "%";
\r
138 c < 16 ? "%0" + c.toString(16) :
\r
139 c < 128 ? p + c.toString(16) :
\r
140 c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :
\r
141 p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)
\r
146 window.encodeURIComponent || (window.encodeURIComponent = function (x) {
\r
147 return ("" + x).replace(/[^!'-*.0-9A-Z_a-z~-]/g, function (s) {
\r
148 var c = s.charCodeAt(0), p = "%";
\r
150 c < 16 ? "%0" + c.toString(16) :
\r
151 c < 128 ? p + c.toString(16) :
\r
152 c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :
\r
153 p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)
\r
159 window.decodeURI || (window.decodeURI = function (x) {
\r
160 return ("" + x).replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig, function (s) {
\r
161 var c = parseInt(s.substring(1), 16);
\r
162 return String.fromCharCode(
\r
164 c < 224 ? (c & 31) << 6 | parseInt(s.substring(4), 16) & 63 :
\r
165 ((c & 15) << 6 | parseInt(s.substring(4), 16) & 63) << 6 | parseInt(s.substring(7), 16) & 63
\r
172 //window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);
\r
179 // replace(RegExp, Function)対応
\r
181 if (window.ActiveXObject ? !Number.prototype.toFixed : (!navigator.taintEnabled && !document.createElement("input").setSelectionRange))
\r
183 var g = String.prototype.replace;
\r
184 String.prototype.replace = function (x, y) {
\r
185 var s = this, z = y;
\r
187 if (y instanceof Function) {
\r
189 if (x instanceof RegExp) {
\r
191 if (x.global || /^\/.*g$/.test(x)) {
\r
193 while ((m = x.exec(s)) != null) {
\r
195 r[r.length] = s.slice(0, i);
\r
196 s = s.slice(i + m[0].length);
\r
197 r[r.length] = y.apply(null, m.concat(i, this));
\r
205 z = y.apply(null, m.concat(m.index, s));
\r
208 var i = s.indexOf(x);
\r
214 return g.call(s, x, z);
\r
219 * Safari の JavaScript の不備
\r
220 * http://nanto.asablo.jp/blog/2006/01/13/209495
\r
222 * web.paulownia.jp - JavaScriptとクロージャ
\r
223 * https://web.archive.org/web/20070526063400/http://web.paulownia.jp/script/oop/closure.html
\r
224 * MacOSX 10.3のsafariにはhasOwnPropertyが実装されていないので、独自に追加する必要があります。
\r
227 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){
\r
228 var proto = this.constructor && this.constructor.prototype,
\r
229 __p__ = proto && proto.__proto__,
\r
233 if( __p__ ) proto.__proto__ = null;
\r
237 if( proto && ( p in proto ) && proto[ p ] !== proto[ p ] ){ // proto[ p ] is NaN
\r
238 proto[ p ] = 0; // different value
\r
239 r = this[ p ] !== this[ p ]; // isNaN?
\r
240 proto[ p ] = v; // set NaN
\r
245 if( proto && p in proto && proto[ p ] === v ){
\r
246 // this と proto に同名で同値が書かれている可能性あり
\r
247 proto[ p ] = v + ' '; // different value
\r
248 r = v === this[ p ];
\r
255 if( __p__ ) proto.__proto__ = __p__;
\r
260 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){
\r
261 var proto = this.constructor && this.constructor.prototype,
\r
262 __p__ = proto && proto.__proto__,
\r
267 if( __p__ ) proto.__proto__ = null;
\r
270 if( proto && p in proto && ( proto[ p ] === v ) ^ isNaN ){ //true + false, false + true
\r
271 // this と proto に同名で同値が書かれている可能性あり
\r
272 proto[ p ] = v + ' '; // different value
\r
273 r = ( v === this[ p ] ) ^ isNaN; // true + false, false + true
\r
280 if( __p__ ) proto.__proto__ = __p__;
\r