最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if Object uses default toString() function or custom overwritten toString() function - Stack Overflow

programmeradmin4浏览0评论

I have a function, which should check if an Object has a toString() function and output it or otherwise return the object. The problem is, it also triggers on plane objects and finally returns [Object object] as a string which obivously looks awfull on the GUI. Is there a way to determine if an Object uses the default toString() method that returns the ugly [Object object] or has an custom toString() function that returns a pretty string.

Here is my current function:

(data: any) => (data != null && typeof data.toString === 'function') ? data.toString() : data;

I have a function, which should check if an Object has a toString() function and output it or otherwise return the object. The problem is, it also triggers on plane objects and finally returns [Object object] as a string which obivously looks awfull on the GUI. Is there a way to determine if an Object uses the default toString() method that returns the ugly [Object object] or has an custom toString() function that returns a pretty string.

Here is my current function:

(data: any) => (data != null && typeof data.toString === 'function') ? data.toString() : data;
Share Improve this question asked Dec 20, 2017 at 10:07 JohnDizzleJohnDizzle 1,3384 gold badges26 silver badges58 bronze badges 1
  • you can check Object.hasOwnProperty('toString') – zabusa Commented Dec 20, 2017 at 10:10
Add a ment  | 

4 Answers 4

Reset to default 9

1) You can pare with Object.prototype.toString. If it is not overridden the references are equal

const obj1 = {};
console.log(obj1.toString === Object.prototype.toString);

const obj3 = { toString() { } };
console.log(obj3.toString === Object.prototype.toString);

2) You can check the existence via hasOwnProperty

const obj1 = {};
console.log(obj1.hasOwnProperty('toString'));

const obj3 = { toString() { } };
console.log(obj3.hasOwnProperty('toString'));

We have two options:

  1. myObj.hasOwnProperty('toString')
  2. myObj.toString !== Object.prototype.toString

I think it is better to use second option, because method using hasOwnProperty isn't so robust. Check out this code snipped:

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(
  "pare with Object.prototype on unchanged object",
  unchanged.toString !== Object.prototype.toString
);
console.log(
  "pare with Object.prototype object from class",
  a.toString !== Object.prototype.toString
);
console.log(
  "pare with Object.prototype object from extended class",
  b.toString !== Object.prototype.toString
);
console.log(
  "pare with Object.prototype object from new Object()",
  c.toString !== Object.prototype.toString
);
console.log(
  "pare with Object.prototype object from object literal",
  d.toString !== Object.prototype.toString
);

Output will be:

"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

"pare with Object.prototype on unchanged object"    false
"pare with Object.prototype object from class"    true
"pare with Object.prototype object from extended class"    true
"pare with Object.prototype object from new Object()"    true
"pare with Object.prototype object from object literal"    true
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // returns true
o.hasOwnProperty('toString');         // returns false
o.hasOwnProperty('hasOwnProperty');   // returns false

prototype properties will return false if you check with hasOwnProperty

None of the answers worked for me. The only one that worked was:

if (obj1.toString) {
  console.log(obj1.toString())
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论