こんにちは,五十嵐です.javascript で動的に作成された変数が定義されているかどうかを確認したいんですが...
<script type="text/javascript">
<!--
for( num = 0; num < 4; num++ ){
eval("var a_" + num + " = new Array(1,2,3,4);");
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br />" );
eval('delete a_' + num);
try{
eval("var tmp = a_" + num);
}catch(e){
document.write( "a_" + num + " Error: " + e + "<br />" );
eval("var a_" + num + " = new Array(11,12,13,14);");
}
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br /><br />" );
}
// -->
</script>こんな感じでしょうか.try-catch だと大仰な気がするんですが,もうちょっとお手軽な方法がないでしょうか.いい方法があったら教えてください.>> javascript の人
実行結果:
【追記 2007/06/20】
"window.hasOwnProperty" というのがあるのですね.知りませんでした.勉強不足がバレバレ...
<script type="text/javascript">
<!--
function _isDefined( obj ){
if( window.hasOwnProperty( obj ) ){
return true;
}
return false;
}
for( num = 4; num < 8; num++ ){
eval("var a_" + num + " = new Array(1,2,3,4);");
if( _isDefined( "a_" + num ) ){
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br />" );
}
eval('delete a_' + num);
if( ! _isDefined( "a_" + num ) ){
document.write( "a_" + num + " is NOT defined.<br />" );
eval("var a_" + num + " = new Array(11,12,13,14);");
}
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br /><br />" );
}
// -->
</script>実行結果:
こっちの方が好みかも.
<script type="text/javascript">
<!--
function _isDefined2( obj ){
if( obj in window ){
return true;
}
return false;
}
for( num = 8; num < 12; num++ ){
eval("var a_" + num + " = new Array(1,2,3,4);");
if( _isDefined2( "a_" + num ) ){
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br />" );
}
eval('delete a_' + num);
if( ! _isDefined2( "a_" + num ) ){
document.write( "a_" + num + " is NOT defined.<br />" );
eval("var a_" + num + " = new Array(11,12,13,14);");
}
document.write( 'a_' + num + ': ' + eval('a_' + num) + "<br /><br />" );
}
// -->
</script>実行結果:
カテゴリ:開発日記
<< [Secure-SBM:014] 各コントロールへの認証 | Main | [Secure-SBM:015] ブックマーク登録(1) >>
いかちょー (2007-06-19 23:50) | コメント(0)| トラックバック(12)
トラックバックURL:
Generic ambien. - Photo of ambien cr. (2010年9月13日 18:24)
Ambien. Ambien online no prescription overnight delivery. Ambien and side eff... 続きを読む
Ambien. - Ambien. (2010年9月13日 23:35)
Ambien. 続きを読む
Ambien. - Ambien empty stomach. (2010年9月16日 22:13)
Ambien latest side effects. Where can i buy ambien for next day delivery. Amb... 続きを読む
Ambien. - Ambien and brain injuries. (2010年9月19日 00:49)
Wellbutrin together with ambien suicide. Ambien latest side effects. Ambien c... 続きを読む
Ambien cr. - Ambien. (2010年9月19日 08:31)
Ambien prescription online overnight us based. Ambien. Ambien overdose. 続きを読む
Ambien dangers. - Ambien. (2010年9月22日 12:50)
Ambien. 続きを読む
Ambien side effects. - Ambien drug screen. (2010年9月25日 12:29)
Ambien sleeping pill. Ambien overnight. Ambien side effects. 続きを読む
Ambien addiction. - Ambien cr. (2010年9月26日 14:25)
Online ambien. Ambien. Ambien and amnesia. 続きを読む
Ambien. - Ambien cr. (2010年9月27日 07:46)
Ambien without prescription. Website for ambien. Ambien cr. Side effects of a... 続きを読む
Adderall overnight no prescription. - Adderall. (2010年9月28日 18:02)
Doctors adderall. Buy adderall online no prescription needed. Adderall purcha... 続きを読む
Ambien vs lunestra. - Ambien online. (2010年10月 2日 10:17)
Ambien cr. Ambien cr addiction. Ambien. 続きを読む
Adderall buy. - Mexican buy adderall. (2010年11月18日 11:43)
Buy adderall online. Buy adderall without a prescription. Buy adderall no pre... 続きを読む
月別アーカイブ
Copyright (C) 2004-2011 Nihon Unisys, Ltd. All Rights Reserved.
Powered by Movable Type Open Source