[JavaScript]windowオブジェクトが保持しているプロパティ一覧を列挙する

ブラウザで動作するJavaScriptでは、windowというグローバル変数が有ります。
この変数は文字通りブラウザのウィンドウを意味する変数です。


window変数がどんなプロパティを持っているか気になったので、確認するスクリプトを作ってみました。

<input id="btnInput" type="button" value="表示" />
< pre id="result"></pre >
 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    // 入力ボタンがクリックされたときのハンドラ
    $("#btnInput").click(function() {
        for( var prop in window ) {
            if ( prop == null || prop == "sessionStorage" ) {
                continue;
            }
 
            // データ型の取得
            var typeStr = typeof window[ prop ];
 
            // 値の取得
            var value = "";
            if ( window[ prop ] != null ) {
                value   = window[ prop ].toString().replaceAll( "\n", "" );
            }
 
            // 直接保持しているプロパティか否かを判定
            var info = "     ";
            if ( window.hasOwnProperty( prop ) == false ) {
                info = "継承 ";
            }
 
            // 取得した情報を出力
            info += "型:" + typeStr + " " + prop + "  " + value + "\n";
            $( "#result" ).text( $( "#result" ).text() + info );
 
        }
    });
 
    String.prototype.replaceAll = function (org, dest){  
      return this.split(org).join(dest);  
    } 
});



スクリプトのポイントを幾つか説明します。




        for( var prop in window ) {


windowオブジェクトが持つプロパティは、上記のforで列挙可能です。これは実際はwindowは連想配列の形になっており、keyがプロパティ名、valueがプロパティ値となっているからです。




            var typeStr = typeof window[ prop ];


データ型はtypeofで取得可能です。typeof演算子の戻り値は文字列型となります。




            if ( window.hasOwnProperty( prop ) == false ) {


取得したプロパティは、自身で定義したものと継承元クラス(javascriptの場合,厳密にはプロトタイプ)で定義済みのものが有ります。各プロパティが、どちらであるかはhasOwnProperty()で確認可能です。



ちなみに、手元の環境(firefox9.0.1)で実行したところ、以下のプロパティを保持していました。
結構沢山有りますね。

     型:object   window  [object Window]
     型:object   navigator  [object Navigator]
     型:object   document  [object HTMLDocument]
     型:object   InstallTrigger  [object Object]
     型:object   console  [object Object]
     型:object   location  file:///C:/test.html
     型:function $  function (a, b) {    return new e.fn.init(a, b, h);}
     型:function jQuery  function (a, b) {    return new e.fn.init(a, b, h);}
     型:object   qlk  [object Object]
     型:function getInterface  function getInterface() {    [native code]}
継承 型:function addEventListener  function addEventListener() {    [native code]}
継承 型:function removeEventListener  function removeEventListener() {    [native code]}
継承 型:function dispatchEvent  function dispatchEvent() {    [native code]}
継承 型:function dump  function dump() {    [native code]}
継承 型:string   name  
継承 型:object   parent  [object Window]
継承 型:object   top  [object Window]
継承 型:object   localStorage  [object Storage]
継承 型:object   globalStorage  [object StorageList]
継承 型:function getComputedStyle  function getComputedStyle() {    [native code]}
継承 型:function getSelection  function getSelection() {    [native code]}
継承 型:function scrollByLines  function scrollByLines() {    [native code]}
継承 型:object   self  [object Window]
継承 型:object   history  [object History]
継承 型:object   locationbar  [object BarProp]
継承 型:object   menubar  [object BarProp]
継承 型:object   personalbar  [object BarProp]
継承 型:object   scrollbars  [object BarProp]
継承 型:object   statusbar  [object BarProp]
継承 型:object   toolbar  [object BarProp]
継承 型:string   status  
継承 型:function close  function close() {    [native code]}
継承 型:function stop  function stop() {    [native code]}
継承 型:function focus  function focus() {    [native code]}
継承 型:function blur  function blur() {    [native code]}
継承 型:number   length  0
継承 型:object   opener  
継承 型:object   frameElement  
継承 型:object   applicationCache  [object OfflineResourceList]
継承 型:function alert  function alert() {    [native code]}
継承 型:function confirm  function confirm() {    [native code]}
継承 型:function prompt  function prompt() {    [native code]}
継承 型:function print  function print() {    [native code]}
継承 型:function showModalDialog  function showModalDialog() {    [native code]}
継承 型:function postMessage  function postMessage() {    [native code]}
継承 型:function atob  function atob() {    [native code]}
継承 型:function btoa  function btoa() {    [native code]}
継承 型:function matchMedia  function matchMedia() {    [native code]}
継承 型:object   screen  [object Screen]
継承 型:number   innerWidth  1136
継承 型:number   innerHeight  311
継承 型:number   scrollX  0
継承 型:number   pageXOffset  0
継承 型:number   scrollY  0
継承 型:number   pageYOffset  0
継承 型:function scroll  function scroll() {    [native code]}
継承 型:function scrollTo  function scrollTo() {    [native code]}
継承 型:function scrollBy  function scrollBy() {    [native code]}
継承 型:number   screenX  290
継承 型:number   screenY  15
継承 型:number   outerWidth  1154
継承 型:number   outerHeight  795
継承 型:function scrollByPages  function scrollByPages() {    [native code]}
継承 型:function sizeToContent  function sizeToContent() {    [native code]}
継承 型:object   content  [object Window]
継承 型:boolean  closed  false
継承 型:object   crypto  [object Crypto]
継承 型:object   pkcs11  
継承 型:object   controllers  [object XULControllers]
継承 型:string   defaultStatus  
継承 型:number   mozInnerScreenX  299
継承 型:number   mozInnerScreenY  185
継承 型:number   scrollMaxX  385
継承 型:number   scrollMaxY  1037
継承 型:boolean  fullScreen  false
継承 型:function back  function back() {    [native code]}
継承 型:function forward  function forward() {    [native code]}
継承 型:function home  function home() {    [native code]}
継承 型:function moveTo  function moveTo() {    [native code]}
継承 型:function moveBy  function moveBy() {    [native code]}
継承 型:function resizeTo  function resizeTo() {    [native code]}
継承 型:function resizeBy  function resizeBy() {    [native code]}
継承 型:function updateCommands  function updateCommands() {    [native code]}
継承 型:function find  function find() {    [native code]}
継承 型:number   mozPaintCount  21
継承 型:function mozRequestAnimationFrame  function mozRequestAnimationFrame() {    [native code]}
継承 型:number   mozAnimationStartTime  1327684982160
継承 型:object   URL  [object MozURLProperty]
継承 型:object   onafterprint  
継承 型:object   onbeforeprint  
継承 型:object   onbeforeunload  
継承 型:object   onhashchange  
継承 型:object   onmessage  
継承 型:object   onoffline  
継承 型:object   ononline  
継承 型:object   onpopstate  
継承 型:object   onpagehide  
継承 型:object   onpageshow  
継承 型:object   onresize  
継承 型:object   onunload  
継承 型:object   ondevicemotion  
継承 型:object   ondeviceorientation  
継承 型:function setTimeout  function setTimeout() {    [native code]}
継承 型:function setInterval  function setInterval() {    [native code]}
継承 型:function clearTimeout  function clearTimeout() {    [native code]}
継承 型:function clearInterval  function clearInterval() {    [native code]}
継承 型:function setResizable  function setResizable() {    [native code]}
継承 型:function captureEvents  function captureEvents() {    [native code]}
継承 型:function releaseEvents  function releaseEvents() {    [native code]}
継承 型:function routeEvent  function routeEvent() {    [native code]}
継承 型:function enableExternalCapture  function enableExternalCapture() {    [native code]}
継承 型:function disableExternalCapture  function disableExternalCapture() {    [native code]}
継承 型:function open  function open() {    [native code]}
継承 型:function openDialog  function openDialog() {    [native code]}
継承 型:object   frames  [object Window]
継承 型:object   onabort  
継承 型:object   onblur  
継承 型:object   oncanplay  
継承 型:object   oncanplaythrough  
継承 型:object   onchange  
継承 型:object   onclick  
継承 型:object   oncontextmenu  
継承 型:object   ondblclick  
継承 型:object   ondrag  
継承 型:object   ondragend  
継承 型:object   ondragenter  
継承 型:object   ondragleave  
継承 型:object   ondragover  
継承 型:object ondragstart  
継承 型:object ondrop  
継承 型:object ondurationchange  
継承 型:object onemptied  
継承 型:object onended  
継承 型:object onerror  
継承 型:object onfocus  
継承 型:object oninput  
継承 型:object oninvalid  
継承 型:object onkeydown  
継承 型:object onkeypress  
継承 型:object onkeyup  
継承 型:object onload  
継承 型:object onloadeddata  
継承 型:object onloadedmetadata  
継承 型:object onloadstart  
継承 型:object onmousedown  
継承 型:object onmousemove  
継承 型:object onmouseout  
継承 型:object onmouseover  
継承 型:object onmouseup  
継承 型:object onmozfullscreenchange  
継承 型:object onpause  
継承 型:object onplay  
継承 型:object onplaying  
継承 型:object onprogress  
継承 型:object onratechange  
継承 型:object onreset  
継承 型:object onscroll  
継承 型:object onseeked  
継承 型:object onseeking  
継承 型:object onselect  
継承 型:object onshow  
継承 型:object onstalled  
継承 型:object onsubmit  
継承 型:object onsuspend  
継承 型:object ontimeupdate  
継承 型:object onvolumechange  
継承 型:object onwaiting  
継承 型:object oncopy  
継承 型:object oncut  
継承 型:object onpaste  
継承 型:object onbeforescriptexecute  
継承 型:object onafterscriptexecute  
継承 型:object mozIndexedDB  [object IDBFactory]
継承 型:object performance  [object Performance]

関連記事

コメントを残す

メールアドレスが公開されることはありません。