JavaScript 4at TECH
JavaScript 4 - 暇つぶし2ch73:デフォルトの名無しさん
15/02/18 17:28:09.82 yNLF/Zre.net
>>65
> ・{hoge: true, foo: false} のように Object 型でオプションを指定したい場合
直感的に思いついた通りに実装すればいいだろ
function test(hoge, foo) {
'use strict';
if (typeof hoge === 'boolean') {
console.log('hoge is', hoge);
} else if (typeof hoge === 'object') {
if (hoge.hasOwnProperty('hoge') && typeof hoge.hoge === 'boolean') {
console.log('hoge is', hoge.hoge);
}
if (hoge.hasOwnProperty('foo') && typeof hoge.foo === 'boolean') {
console.log('foo is', hoge.foo);
}
}
}
test(true, false);
test({hoge: true, foo: false});
test({hoge: 2});
test({hage: 2});
> ・配列のようなオブジェクト(NodeList等)を判定したい場合
「ような」なんだから
if (hoge.length > 0) {
// 配列の「ような」オブジェクト
}
で問題ないだろ


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch