2016-08-03 80 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

不是字符串,好吗? –

相关问题