2017-06-08 84 views
2

Im與Three.jsjavascript一起使用。 當我的代碼執行此:在Javascript中獲取對象的名稱作爲字符串

console.log(this.scene.children[1]) 

我得到這個在我的Chrome的控制檯: enter image description here

我怎樣才能獲取對象(「WidgetsRuler」)作爲一個字符串的名字嗎? 我沒有看到保存此信息的任何屬性。

回答

2

好吧,我解決它使用:

console.log(this.scene.children[1].constructor.name) 
+0

你可以標記自己的答案作爲答案 –

+0

它說我必須等待2天 – Rashomon

1

我相信你應該能夠使用prototype來實現這一目標:

Object.prototype.toString 

如:

Console.Log(this.scene.children[1].prototype.toString()) 

如果做不到這一點,你可以嘗試constructor

console.log(this.scene.children[1].constructor.name) 
+0

謝謝sk93!控制檯說'this.scene.children [1] .prototype'是undefined:/ – Rashomon

+0

我只是編輯我的答案,而不是顯示「構造函數」,但你打敗了我:) – Sk93

+1

謝謝Sk93!當我已經發布時,我總是找到答案:') – Rashomon

相關問題