2012-01-31 151 views
2

我想要做類似下面的代碼片段在紅寶石有沒有辦法在javascript中獲取返回值的類型?

a = MyClass.new 

a.class 

#=> MyClass 
a.methods 

#=> list all available methods for object a or instances methods for MyClass 

我可以做類似的事情在JavaScript?也希望聽到你的建議提供任何更好的方法來獲取對象類型的,什麼是可用於該對象的方法和屬性

回答

1

var a = new MyClass(); 
console.log(a.constructor); 
// => MyClass 
console.log(a.constructor.prototype); 
// => [object Object] with methods of that class 
// (does not include inherited methods) 
+0

這正是我想要的,雖然@benekastah的方式是發燒友,但我認爲這是一件好事,知道什麼是對underscore.js引擎蓋 – mko 2012-01-31 05:27:35

2

我會強烈建議underscore.js這個任務。

下劃線是JavaScript的一個實用工具,帶庫,它提供了一個 很多函數式編程的支持,你會期望在 的Prototype.js(或Ruby)的。

你正在尋找的方法是functions,也被別名爲methods(鏈接應該帶你到它)。

var a = new MyClass() 

a instanceof MyClass // => true 
a.constructor === MyClass // => true 

_(a).methods() // Lists all methods that are members of a 
+0

良好的號召下運行,沒有必要(很差)自己實施這種事情。 – 2012-01-31 04:26:25

+0

underscore.js看起來很酷,還有很長的路要走,我會在後面深入探究 – mko 2012-01-31 05:28:35

+0

@ muistooshort如何讓.methods()顯示繼承的方法? – mko 2012-01-31 06:01:15

相關問題