- JavaScript 4©2ch.net
72 :デフォルトの名無しさん[sage]:2015/02/18(水) 17:28:09.82 ID:yNLF/Zre - >>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) { // 配列の「ような」オブジェクト } で問題ないだろ
|