1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
\r
2 <html xmlns:v="urn:schemas-microsoft-com:vml"><!-- InstanceBegin template="/Templates/base.dwt" codeOutsideHTMLIsLocked="false" -->
\r
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
\r
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
\r
6 <meta http-equiv="Content-Language" content="ja">
\r
7 <meta http-equiv="Content-Script-Type" content="text/javascript">
\r
8 <meta http-equiv="Content-Style-Type" content="text/css">
\r
9 <meta http-equiv="imagetoolbar" content="no">
\r
10 <link rel="stylesheet" type="text/css" href="../stylesheets/site.css">
\r
11 <link rel="stylesheet" type="text/css" href="../stylesheets/peta.common.css">
\r
12 <link rel="stylesheet" type="text/css" href="../stylesheets/system.css">
\r
13 <!-- InstanceBeginEditable name="doctitle" -->
\r
14 <title>[js]type | pettanR</title>
\r
15 <!-- InstanceEndEditable -->
\r
16 <!-- InstanceBeginEditable name="head" -->
\r
18 dt { font-weight:bold;}
\r
19 .type-is-String { color:red;}
\r
20 .type-is-Number { color:#f6f;}
\r
21 .type-is-Function { color:green;}
\r
22 .type-is-Array, .type-is-Object {
\r
25 .type-is-Boolean, .type-is-null, .type-is-undefined {
\r
28 .type-is-HTMLElement {
\r
32 <!-- InstanceEndEditable -->
\r
36 <div id="general-content">
\r
37 <div id="outer-wrapper">
\r
40 <div class="base-content-width" style="position:relative;">
\r
41 <h1><a href="../index.html">pettanR</a></h1>
\r
42 <!-- global navi -->
\r
43 <div id="global-navi">
\r
44 <noscript id="noscript-alert">You need to have JavaScript enabled.</noscript>
\r
49 <div id="inner-wrapper" class="base-content-width">
\r
50 <!-- InstanceBeginEditable name="main_content" -->
\r
53 <div id="breadcrumb"><a href="index.html">テスト インデックス</a> > javascript のための型判定</div>
\r
54 <h2>javascript のための型判定</h2>
\r
55 <p>以下の表は js によっ書き出されています.Array 型 と HTMLElement 型 の判定が面倒です.また、NodeList 型が返るか?はブラウザ毎の実装差があるようです.</p>
\r
56 <dl id="output" class="dl-table clearfix"></dl>
\r
58 <p>以下の記事を参考にしました.</p>
\r
60 <a href="http://d.hatena.ne.jp/cheesepie/20091122/1258897939" target="_blank">JavaScriptで型判定いろいろ - Cheese Pie</a>
\r
61 このページの型チェック用コードのベースにした記事です.
\r
64 <a href="http://zombiebook.seesaa.net/article/118457190.html" target="_blank">Javascript:element の判定とそれに関わる諸事: Script雑感</a>
\r
65 isElement の判定の参考にしました.ie5 や ns6 といった古いブラウザでもチェックされていてすばらしいです.
\r
68 <p>2012-03-18 file.read() のための DeepCopy を書きたくて、型判定方法について調べる.</p>
\r
70 <!-- InstanceEndEditable -->
\r
75 <div id="footer-content"><!-- #BeginLibraryItem "/Library/footer.lbi" -->
\r
78 <div id="footer-item-container" class="base-content-width clearfix">
\r
79 <div class="footer-item-wrapper">
\r
80 <div class="footer-item">
\r
81 <h2>ぺったんR にようこそ</h2>
\r
89 <li><a href="index.html">Test Index</a></li>
\r
93 <div class="footer-item-wrapper">
\r
94 <div class="footer-item">
\r
97 <li><a href="http://sourceforge.jp/projects/pettanr/wiki/ForAuthors" rel="me" target="_blank">作家向けドキュメント</a></li>
\r
98 <li>絵師向けドキュメント</li>
\r
99 <li><a href="http://sourceforge.jp/projects/pettanr/wiki/ForDevelopers" rel="me" target="_blank">開発者向けドキュメント</a></li>
\r
100 <li><a href="http://sourceforge.jp/projects/pettanr/wiki/ForWebmasters" rel="me" target="_blank">編集長向けドキュメント</a></li>
\r
104 <div class="footer-item-wrapper">
\r
105 <div class="footer-item">
\r
108 <li><a href="http://pettanr.heroku.com/" rel="me" target="_blank">pettanr.heroku.com</a></li>
\r
109 <li><a href="../work.html" rel="console">ユーザーコンソール</a></li>
\r
111 <h2>Ajax contents</h2>
\r
113 <li><a href="../help/jp.xml">help | jp</a></li>
\r
117 <div class="footer-item-wrapper" style="width:170px;">
\r
118 <div class="footer-item" style="padding-right:0;">
\r
123 <li><a href="../diary/itozyun.xml">itozyun's diary</a></li>
\r
131 <div id="copyright">Copyright (c) 2011-2012 pettanR team, some rights reserved.</div>
\r
132 </div><!-- #EndLibraryItem -->
\r
134 <script type="text/javascript">
\r
135 var has_server_support = false;
\r
137 <!-- InstanceBeginEditable name="script" -->
\r
138 <script type="text/javascript">
\r
140 * Decision type of variable.
\r
145 isObject : function(v) {
\r
146 return typeof v === 'object';
\r
148 isFunction : function(v) {
\r
149 return typeof v === 'function';
\r
151 isArray : function(v) {
\r
152 return Object.prototype.toString.call(v) === "[object Array]";
\r
154 isBoolean : function(v) {
\r
155 return typeof v === 'boolean';
\r
157 isString : function(v) {
\r
158 return typeof v === 'string';
\r
160 isNumber : function(v) {
\r
161 return typeof v === 'number';
\r
163 isHTMLElement : ( 'HTMLElement' in window ?
\r
165 return v instanceof HTMLElement
\r
168 if( Type.isObject(v) === false ){
\r
172 if(v && v.nodeType === 1){
\r
174 r = v.cloneNode(false);
\r
178 if(r === v) return false;
\r
181 return r.nodeType === 1;
\r
188 isElementCollection : function(v) {
\r
189 return (Object.prototype.toString.call(v) === "[object HTMLCollection]");
\r
191 isNull : function(v) {
\r
194 isUndefined : function(v) {
\r
195 return typeof v === 'undefined';
\r
199 * @param {Any} v The variable
\r
200 * @return {String} type of the variable
\r
202 getType : function(v) {
\r
203 if(Type.isNull(v)) {
\r
206 if(Type.isUndefined(v)) {
\r
207 return "undefined";
\r
209 if(Type.isHTMLElement(v)) {
\r
210 return "HTMLElement";
\r
212 return Object.prototype.toString.call(v).split(" ")[1].replace("]", "");
\r
216 var output = document.getElementById( 'output'),
\r
217 testNameArray = [ '"abc"', 'true', '100', '[]', '{}', 'function(){}', 'undefined', 'null', 'document.getElementById( "output")', 'document.getElementById("notExist")', 'document.getElementsByTagName("div")', 'document.body.childNodes', 'document.createTextNode( "hello" )'],
\r
218 testValueArray = [ "abc", true, 100, [], {}, function(){}, undefined, null, output, document.getElementById("notExist"), document.getElementsByTagName("div"), document.body.childNodes, document.createTextNode( "hello" )],
\r
220 for(var i=0, l=testValueArray.length; i<l; ++i){
\r
221 dt = document.createElement( 'dt');
\r
222 dd = document.createElement( 'dd');
\r
223 output.appendChild( dt );
\r
224 output.appendChild( dd );
\r
225 type = Type.getType(testValueArray[i]);
\r
226 dt.appendChild( document.createTextNode( type ));
\r
227 dt.className = 'type-is-' + type;
\r
228 dd.appendChild( document.createTextNode( testNameArray[i]));
\r
232 <!-- InstanceEndEditable -->
\r
234 <!-- InstanceEnd --></html>
\r