2016-08-03 21 views
3

如果我有任何對象,也許new MyObject(),我wnat顯示所有內部屬性。我該怎麼做?如何在JavaScript中將任何對象顯示爲字符串?

當使用alert(new MyObject())時,結果爲[object Object]。但我想要所有的內在屬性。例如...

var MyObject = function() { 
    this.prop1 = "Hello World"; 
    this.prop2 = "LOL"; 
    this.recursive = this; 
    this.func = function() { return "func return"; } 
} 
alert(new MyObject()); 

在這種情況下,我該怎麼做才能顯示{ prop1 = "Hello World", prop2 = "LOL", etc... }

+2

'JSON.stringify(object)'...? – Damon

+1

@Damon這是一個循環引用。 –

+1

通常你使用'JSON.stringify(new MyObject())'。請注意,由於循環引用'this.recursive = this',它將不起作用,因爲它會永遠打印。要解決這個問題,你可以使用第二個參數,參見[這個答案](http://stackoverflow.com/a/9653082/3149020)。 –

回答

2

你可以寫這個功能,任何對象轉換爲string

JSFiddle

function ToString(obj) { 
    clearTimeout(window.ToStringTimeout); 

    var result; 
    var ident = arguments.length >= 2 ? arguments[1] : undefined; 

    if (obj == null) { 
     result = String(obj); 
    } 

    if (!result) { 
     window.ToStringRecursive = window.ToStringRecursive ? window.ToStringRecursive : []; 
     if (ToStringRecursive.indexOf(obj) >= 0) { 
      result = obj ? (typeof(obj) == "string" ? "\"" + obj + "\"" : obj.toString()) : obj; 
     } else { 
      ToStringRecursive.push(obj); 
     } 
     if (!result) { 
      switch (typeof obj) { 
       case "string": 
        result = '"' + obj + '"'; 
        break; 
       case "function": 
        result = obj.name || obj.toString(); 
        break; 
       case "object": 
        var indent = Array(ident || 1).join('\t'), 
         isArray = Array.isArray(obj); 
        result = '{[' [+isArray] + Object.keys(obj).map(
         function(key) { 
          return '\n\t' + indent + key + ': ' + ToString(obj[key], (ident || 1) + 1); 
         }).join(',') + '\n' + indent + '}]' [+isArray]; 
        break; 
       default: 
        result = obj.toString(); 
        break; 
      } 
     } 
    } 

    window.ToStringTimeout = setTimeout(function() { 
     delete window.ToStringTimeout; 
     delete window.ToStringRecursive; 
    }, 100); 

    return result; 
} 

,並使用此:

console.log(ToString(new MyObject())); 

爲了證明這一點:

{ 
    prop1: "Hello World", 
    prop2: "LOL", 
    recursive: [object Object], 
    func: function() { return "func return"; } 
} 

檢查...當任何財產是遞歸的這一再顯示,因爲這是無限的。

0

像這樣:

var obj = {type:"Fiat", model:"500", color:"white"}; 
 
var str = ''; 
 
    
 
for (var p in obj) { 
 
    str = str + p + " = " + obj[p] + ','; 
 
} 
 
console.log(str);

+0

這不是OP想要的。 –

+0

恩,真的。那個怎麼樣? –

+1

這只是給了對象鍵一個領先的逗號... – Damon

0

var MyObject = function() { 
 
    this.prop1 = "Hello World"; 
 
    this.prop2 = "LOL"; 
 
    this.recursive = this; 
 
    this.func = function() { 
 
    return this.recursive.prop1 + "," + this.recursive.prop2; 
 
    } 
 
} 
 
var output = new MyObject(); 
 
alert(output.func());

+1

你讀過這個問題了嗎? –

+0

是的,他想獲得對象值。因此,我創建實例和調用函數來獲取值。 –

+1

不,他們希望將其打印爲對象字面值。 –

2

使用:

var MyObject = function() { 
    this.prop1 = "Hello World"; 
    this.prop2 = "LOL"; 
    this.recursive = this; 
    this.func = function() { return "func return"; } } 

console.log(eval(new MyObject())); 

結果是:

{ prop1: 'Hello World', 
    prop2: 'LOL', 
    recursive: [Circular], 
    func: [Function] } 
+1

不是字符串,好嗎? –

相關問題