OSDN Git Service

2dcc94314b1762e827657ade62c8dd04c0687e73
[pettanr/clientJs.git] / 0.6.x / js / 00_core / 00_builtin.js
1 /* \r
2  * ビルトインオブジェクトに拡張したい10のメソッド\r
3  * http://d.hatena.ne.jp/ofk/20080922/1222047483\r
4  */\r
5 \r
6 Function.prototype.apply || (Function.prototype.apply = function (x, y) {\r
7         var a, i, r, j;\r
8         x = x || window;\r
9         y = y || [];\r
10         \r
11         // apply 内で apply を呼んだ場合に備える\r
12         if( x === window ){\r
13                 x.__apply = void 0;\r
14         } else {\r
15                 if( x.constructor && x.constructor.prototype.__apply ){\r
16                         delete x.constructor.prototype.__apply;\r
17                 } else\r
18                 if( x.__apply ) delete x.__apply;\r
19         };\r
20         \r
21         x.__apply = this;\r
22         if (!x.__apply) x.constructor.prototype.__apply = this;\r
23         j = y.length;\r
24         switch (j) {\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
35                 default:\r
36                         a = [];\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
42                         break;\r
43         };\r
44         // ie5\r
45         if( x === window ){\r
46                 x.__apply = void 0;\r
47         } else {\r
48                 if( x.constructor && x.constructor.prototype.__apply ){\r
49                         delete x.constructor.prototype.__apply;\r
50                 } else\r
51                 if( x.__apply ) delete x.__apply;\r
52         };\r
53         return r;\r
54 });\r
55 Function.prototype.call || (Function.prototype.call = function () {\r
56         var a = arguments, x = a[0], y = [], i = 1, j = a.length;\r
57         for (; i < j; ++i)\r
58                 y[i - 1] = a[i];\r
59         return this.apply(x, y);\r
60 });\r
61 \r
62 Array.prototype.pop || (Array.prototype.pop = function () {\r
63         var r = this[this.length - 1];\r
64         --this.length;\r
65         return r;\r
66 });\r
67 Array.prototype.push || (Array.prototype.push = function () {\r
68         var a = arguments, i = 0, j = a.length, l = this.length;\r
69         for (; i < j; ++i)\r
70                 this[l + i] = a[i];\r
71         return this.length;\r
72 });\r
73 Array.prototype.shift || (Array.prototype.shift = function () {\r
74         var r = this[0], i = 1, j = this.length;\r
75         for( ; i < j; ++i)\r
76                 this[i - 1] = this[i];\r
77         --this.length;\r
78         return r;\r
79 });\r
80 Array.prototype.unshift || (Array.prototype.unshift = function () {\r
81         var a = arguments, l = a.length, j = this.length += l - 1, i = j;\r
82         for (; i >= l; --i)\r
83                 this[i] = this[i - l];\r
84         for (i = 0; i < l; ++i)\r
85                 this[i] = a[i];\r
86         return j;\r
87 });\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),i,j;\r
90         if (s > 0) {\r
91                 for (i = this.length - 1, j = x + y; i >= j; --i)\r
92                         this[i + s] = this[i];\r
93         }\r
94         else if (s < 0) {\r
95                 for (i = x + y, j = this.length; i < j; ++i)\r
96                         this[i + s] = this[i];\r
97                 this.length += s;\r
98         }\r
99         for (i = 2, j = a.length; i < j; ++i)\r
100                 this[i - 2 + x] = a[i];\r
101         return r;\r
102 });\r
103 \r
104 /*\r
105  * original\r
106  * JavaScript 1.6, Array.indexOfを下位互換実装する\r
107  * http://www.inazumatv.com/contents/archives/7965\r
108  */\r
109 \r
110 Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, fromIndex ){\r
111         var l = this.length >>> 0, i;\r
112         \r
113         if( l === 0 ) return -1;\r
114         \r
115         if( fromIndex ){\r
116             i = fromIndex || 0;\r
117             i = i === -Infinity ? 0 : ( i < 0 ? -i : i ) | 0; // Math.floor\r
118             if( l <= i ) return -1;\r
119         };\r
120 \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
123         };\r
124         return -1;\r
125         });\r
126 \r
127 \r
128 /*\r
129  * Window\r
130  * by http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html\r
131  */\r
132 \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
137                 return (\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
142                 ).toUpperCase();\r
143         });\r
144 });\r
145 \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
149                 return (\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
154                 ).toUpperCase();\r
155         });\r
156 });\r
157 \r
158 // 手抜き\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
163                         c < 128 ? c :\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
166                 );\r
167         });\r
168 });\r
169 */\r
170 \r
171 \r
172 //window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);\r
173 \r
174 \r
175 /*\r
176  * String\r
177  */\r
178 \r
179 // replace(RegExp, Function)対応\r
180 /*\r
181 if (window.ActiveXObject ? !Number.prototype.toFixed : (!navigator.taintEnabled && !document.createElement("input").setSelectionRange))\r
182         (function () {\r
183                 var g = String.prototype.replace;\r
184                 String.prototype.replace = function (x, y) {\r
185                         var s = this, z = y;\r
186                         // 第二引数が関数\r
187                         if (y instanceof Function) {\r
188                                 // 第一引数が正規表現\r
189                                 if (x instanceof RegExp) {\r
190                                         // その上、グローバルマッチ\r
191                                         if (x.global || /^\/.*g$/.test(x)) {\r
192                                                 var r = [], m;\r
193                                                 while ((m = x.exec(s)) != null) {\r
194                                                         var i = m.index;\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
198                                                 }\r
199                                                 r[r.length] = s;\r
200                                                 return r.join("");\r
201                                         }\r
202                                         var m = x.exec(s);\r
203                                         if (!m)\r
204                                                 return s;\r
205                                         z = y.apply(null, m.concat(m.index, s));\r
206                                 }\r
207                                 else {\r
208                                         var i = s.indexOf(x);\r
209                                         if (i < 0)\r
210                                                 return s;\r
211                                         z = y(x, i, s);\r
212                                 }\r
213                         }\r
214                         return g.call(s, x, z);\r
215                 };\r
216         })(); */\r
217         \r
218 /*\r
219  * Safari の JavaScript の不備 \r
220  * http://nanto.asablo.jp/blog/2006/01/13/209495\r
221  * \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
225  */\r
226 /*\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
230                         v     = this[ p ],\r
231                         r     = false;\r
232                 \r
233                 if( __p__ ) proto.__proto__ = null;\r
234                 \r
235                 if( p in this ){\r
236                         if( v !== v ){\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
241                                 } else {\r
242                                         r = true;\r
243                                 };\r
244                         } else\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
249                                 proto[ p ] = v;\r
250                         } else {\r
251                                 r = true;\r
252                         };\r
253                 };\r
254                 \r
255                 if( __p__ ) proto.__proto__ = __p__;\r
256                 \r
257                 return r;\r
258   }); */\r
259 /*\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
263                         r     = false,//!!( __p__ && ( proto.__proto__ = null ) )\r
264                         _pro_, v, isNaN;\r
265                 \r
266                 if( __p__ ) proto.__proto__ = null;\r
267                 if( this.__proto__ ){\r
268                         _pro_ = this.__proto__;\r
269                         this.__proto__ = null;\r
270                 };\r
271                 \r
272                 if( p === '__proto__' ){\r
273                         r = !!_pro_;\r
274                 } else {\r
275                         v     = this[ p ];\r
276                         isNaN = v !== v;                \r
277                         \r
278                         if( p in this ){\r
279                                 if( proto && p in proto && ( proto[ p ] === v ) ^ isNaN ){ //true + false, false + true\r
280                                         // this と proto に同名で同値が書かれている可能性あり\r
281                                         proto[ p ] = v + ' '; // different value\r
282                                         r = ( v === this[ p ] ) ^ isNaN; // true + false, false + true\r
283                                         proto[ p ] = v;\r
284                                 } else {\r
285                                         r = true;\r
286                                 };\r
287                         };                      \r
288                 };\r
289 \r
290                 if( __p__ ) proto.__proto__ = __p__;\r
291                 if( _p_ ) this.__proto__ = _pro_;\r
292                 return r;\r
293   }); */