简体   繁体   中英

How to print only the `this` object's name , and not it's contents in javascript?

consider the function:

 function foo() { console.log(this); } foo(); 

How to print only the this object's name (window or global....etc), and not it's contents?

You can print an objects name in js with obj.constructor.name .

 function foo() { console.log(this.constructor.name) } foo() 

Will print Window

You can use this.constructor.name :

 function foo() { console.log(this.constructor.name); } foo(); 

by constructor name. console.log(this.constructor.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