我有一个函数,它应该检查一个Object是否有一个toString()
函数并输出它或以其他方式返回该对象。问题是,它还在平面对象上触发,最后返回[Object object]
作为一个字符串,在GUI上看起来很糟糕。有没有办法确定一个Object是否使用返回丑陋toString()
的默认[Object object]
方法,或者是否有一个返回漂亮字符串的自定义toString()
函数。
这是我目前的职能:
(data: any) => (data != null && typeof data.toString === 'function') ? data.toString() : data;
答案 0 :(得分:1)
1)您可以与Object.prototype.toString
进行比较。如果没有覆盖引用是相等的
const obj1 = {};
console.log(obj1.toString === Object.prototype.toString);
const obj3 = { toString() { } };
console.log(obj3.toString === Object.prototype.toString);

2)您可以通过hasOwnProperty
const obj1 = {};
console.log(obj1.hasOwnProperty('toString'));
const obj3 = { toString() { } };
console.log(obj3.hasOwnProperty('toString'));

答案 1 :(得分:0)
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // returns true
o.hasOwnProperty('toString'); // returns false
o.hasOwnProperty('hasOwnProperty'); // returns false
如果您使用hasOwnProperty
进行检查,原型属性将返回false
答案 2 :(得分:0)
我们有两种选择:
myObj.hasOwnProperty('toString')
myObj.toString !== Object.prototype.toString
我认为最好使用第二个选项,因为使用hasOwnProperty
的方法并不是那么健壮。查看此代码剪切:
class MyObj {
toString() {
return "myObj";
}
}
class MyObjEx extends MyObj {
something() {}
}
var a = new MyObj();
var b = new MyObjEx();
var c = new Object();
c.toString = () => "created by new Object()";
var d = { test: "test", toString: () => "created with object literal" };
var unchanged = { test: "test" };
console.log(
"hasOwnProperty with 'native' toString method",
unchanged.hasOwnProperty("toString")
);
console.log("hasOwnProperty object from class", a.hasOwnProperty("toString"));
console.log(
"hasOwnProperty object from extended class",
b.hasOwnProperty("toString")
);
console.log(
"hasOwnProperty object from new Object()",
c.hasOwnProperty("toString")
);
console.log(
"hasOwnProperty object from object literal",
d.hasOwnProperty("toString")
);
console.log(
"compare with Object.prototype on unchanged object",
unchanged.toString !== Object.prototype.toString
);
console.log(
"compare with Object.prototype object from class",
a.toString !== Object.prototype.toString
);
console.log(
"compare with Object.prototype object from extended class",
b.toString !== Object.prototype.toString
);
console.log(
"compare with Object.prototype object from new Object()",
c.toString !== Object.prototype.toString
);
console.log(
"compare with Object.prototype object from object literal",
d.toString !== Object.prototype.toString
);
输出将是:
"hasOwnProperty with 'native' toString method" false
"hasOwnProperty object from class" false
"hasOwnProperty object from extended class" false
"hasOwnProperty object from new Object()" true
"hasOwnProperty object from object literal" true
"compare with Object.prototype on unchanged object" false
"compare with Object.prototype object from class" true
"compare with Object.prototype object from extended class" true
"compare with Object.prototype object from new Object()" true
"compare with Object.prototype object from object literal" true
答案 3 :(得分:0)
没有一个答案对我有用。唯一可行的是:
if (obj1.toString) {
console.log(obj1.toString())
}