如何在JavaScript中将任何对象显示为字符串
How can I show any object as string in JavaScript?
如果我有任何对象,可能是new MyObject()
,并且我不显示所有内部属性。我该怎么做?
当使用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... }
您可以编写此函数并将任何对象转换为string
。
Look JSFiddle
////For NodeJS remove comment below:
//var window = { };
function ToString(obj) {
clearTimeout(window.ToStringTimeout);
var result;
var ident = arguments.length >= 2 ? arguments[1] : undefined;
if (obj == null) {
result = String(obj);
}
var objString;
try {
objString = obj.toString();
} catch (err1) {
try {
objString = String(obj);
} catch (err2) {
try {
objString = obj + "";
} catch (err3) {
objString = "ERROR CONVERT STRING";
}
}
}
if (!result) {
window.ToStringRecursive = window.ToStringRecursive ? window.ToStringRecursive : [];
if (window.ToStringRecursive.indexOf(obj) >= 0) {
result = obj ? (typeof(obj) == "string" ? "'"" + obj + "'"" : objString) : obj;
} else {
window.ToStringRecursive.push(obj);
}
if (!result) {
switch (typeof obj) {
case "string":
result = '"' + obj + '"';
break;
case "function":
result = obj.name || objString;
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 = objString;
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"; }
}
观察。。。当任何属性是递归的时,这不会再次显示,因为这是无限的。
使用此:
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] }
像这样:
var obj = {type:"Fiat", model:"500", color:"white"};
var str = '';
for (var p in obj) {
str = str + p + " = " + obj[p] + ',';
}
console.log(str);
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());
效率更高。。。使用JSON.stringify
和替换程序来逐出递归属性:
function toString(instance, space = ' ') {
const objects = [];
const replacer = (key, value) => {
if (typeof value === 'object' && value !== null) {
if (objects.findIndex(object => object === value) >= 0) {
return ({}).toString();
}
objects.push(value);
}
return value;
};
return JSON.stringify(instance, replacer, space);
}
相关文章:
- javascript对象显示为null,但object.properties返回数据
- Angular2 - json 对象显示名称,但在列表中单击时获取 id
- 对象 对象显示在 td 中
- 通过 html 图像元素对象显示图像
- 跨 ajax 请求到服务器返回的 json 对象显示错误意外令牌:
- 将 ajax 请求的 JSON 对象显示在 HighCharts 中
- 使用日期对象显示带有UTC日期时间输入的本地时间
- 控制台日志事件对象显示的对象属性与其应有的属性不同
- js/jquery对象显示只是一个快速的闪光
- 警告Javascript数组对象显示为空
- 如何用json对象显示angularjs ng repeat
- 数组中的对象显示为[object object]
- 如何从JavaScript数组/对象显示图像?从第一个图像开始,然后点击到下一个
- 对象显示对象而不是按钮
- 为什么我的对象显示为&;[object object]&;
- Json对象显示值1
- Json对象显示在html中
- 如何在HTML中将JSON对象显示为下拉菜单的选项,对所有对象使用通用的JavaScript函数
- 为什么控制台显示对象内容时,对象显示为未定义
- 如何在JavaScript中将任何对象显示为字符串