所以在 ES6 中,有
.constructor.name
可以用于任何对象来获取其类名。
ES5 中有什么东西可以做到这一点吗?
(注:在评论者提到升级浏览器等之前,这个问题是关于 Fitbit 的开发,它使用 jerryscript,一个基于 ES5.1 的轻量级引擎)
与 ES6 相同:
function Tree(name) {
this.name = name;
}
const theTree = new Tree("Redwood");
console.log(theTree.constructor.name);