[JS] 列出Java Script 物件類型範例

[JS] 列出Java Script 物件類型範例

程式代碼:

        var test = {};
        (function(obj){
            obj.do = function(sub){
                    if(console){                            
                        console.log("__proto__        : " + sub.__proto__);
                        console.log("prototype        : " + sub.prototype);
                        console.log("typeof           : " + typeof sub);                        
                        console.log("isArray          : " + (sub instanceof Array).toString());
                        console.log("toString         : " + sub.toString());                                
                        console.log("obj              : " + sub);
                        console.log("constructor name : " + sub.constructor.name);    
                    }
            }
            
            obj.subdo = function(){
                
            }
            
            obj.subObj = {}
            
            obj.arr = new Array;
            obj.arr[0] = 0;
            obj.arr[1] = 1;
        })(test);

測試案例:

<input type="button" value="testFunc" onclick="test.do(test.subdo)" />
 
<input type="button" value="testString" onclick="test.do('1234')" />
 
<input type="button" value="testInt" onclick="test.do(1234)" />
 
<input type="button" value="testObject" onclick="test.do(test.subObj)" />
 
<input type="button" value="testArray" onclick="test.do(test.arr)" />
 
<input type="button" value="testWindow" onclick="test.do(window)" />
 
<input type="button" value="testHTMLInputElement" onclick="test.do(this)" />

 

結果 snapshort from chrome:

Func

image

String

image

Int

image

Object

image

Array

image

Window

image

HTMLInputElement

image

超詳細參考內容推薦:http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript