简体   繁体   中英

How to get 'class' name like Chrome's dev console?

Below is a piece of code evaluated in Chrome's console. A function constructor is created anonymously and used to construct an object. Chrome happily prints the real constructor's name 'Foo'. But, I can't find a way to get it using standard JS. Where's the magic?

> var exports = {}
undefined
> (function(){var Foo = function() {}; Foo.prototype = "";  exports.bar = Foo;})()
undefined
> obj = new exports.bar();
Foo {}
> obj.constructor.name
'Object'
> obj.__proto__
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
> obj.__proto__.name
undefined
> obj
Foo {}

To get Foo you can exports.bar.name

if (obj.__proto__.isPrototypeOf(exports.bar)) {
  console.log(exports.bar.name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM