2016-03-15 64 views
0

這個想法是獲得類的方法/屬性列表。例如,我有這個類:Javascript ES6檢索類的成員

// foo.js 
class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    //, 
    meh() { 
     return 'bar'; 
    } 
} 
module.exports = FooController; 

現在我想檢索FooController的成員。這應該很容易對JavaScript 平原對象來完成,但不能與

// index.js 
var Foo = require('foo'); 
var foo = new Foo(); 

// inspect 
console.log(foo); // expected: { bar, meh }, but got {} 

// struggle with lodash 
var _ = require('lodash');  
console.log(_.keys(foo)); // expected ['bar', 'meh'], but got [] 

任何想法?預先感謝您的任何幫助或建議。

+0

* bar是故意背後的逗號嗎?那裏出現語法錯誤。 – Randy

+0

對不起,這是錯字 - 讓我編輯它,並感謝更正! – Adiono

回答

1

您應該查詢prototypeFoo()類實例的對象:

// index.js 
var Foo = require('foo'); 
var foo = new Foo(); 

var _ = require('lodash');  
console.log(Object.getOwnPropertyNames((Object.getPrototypeOf(foo)))); // shows ['bar', 'meh', 'constructor'] 
// or 
console.log(Object.getOwnPropertyNames(foo.__proto__)); // shows ['bar', 'meh', 'constructor'] 

建議的_.keys(foo)僅返回對象的擁有屬性(bar()meh()被繼承),其實際爲空[]

爲了獲取你所需要的方法:

  • 訪問實例
  • prototype對象枚舉prototype的屬性。由於所需的屬性不可枚舉(Object.keys()_.keys()不起作用),因此需要使用Object.getOwnPropertyNames()

查看更多about Object.getPrototypeOf()Object.getOwnPropertyNames()
查看this fiddle的完整示例。

+0

在我的結尾處,它顯示'[]'而不是'['bar','meh']' – Adiono

+0

@Adiono糟糕。答案已更新。 –

+0

酷!它正在工作,謝謝德米特里! – Adiono

0

選擇1:你可以打印class constructor,至極給出了期望的結果:

class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    meh() { 
     return 'bar'; 
    } 
} 


var foo = new FooController(); 

console.log(foo.constructor); 

function class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    meh() { 
     return 'bar'; 
    } 
} 

選項2:您可以只打印class object不綁定它:

class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    meh() { 
     return 'bar'; 
    } 
} 

console.log(FooController); 

function class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    meh() { 
     return 'bar'; 
    } 
} 
+1

問題是如何檢索成員,而不是如何打印藍圖。 – zeroflagL

+0

是的,@zeroflagL是對的!我希望會員不是藍圖。 – Adiono

2

class ES6中的es只是糖編碼,定義了ES5中函數prototype的方法。因此,對於你的例子:

class FooController { 
    constructor() { 
    } 
    *bar(next) { 
     yield next; 
     return 'meh'; 
    } 
    //, 
    meh() { 
     return 'bar'; 
    } 
} 
module.exports = FooController; 

的ES5相當於是:

var FooController = (function() { 
    function FooController() { } // This method will act as our constructor 

    // All methods defined on the prototype are equivalent with the 
    // so called class methods in ES6 
    FooController.prototype.bar = *function(next) { yield next; return 'meh'; }; 

    FooController.prototype.meh = function() { return 'bar'; }; 

    return FooController; 
})(); 

訪問的 「公共」 的方法關閉FooController您可以:

  1. 訪問原型明確狀所以:FooController.prototype

    console.log(FooController.prototype.meh()) // => 'bar'

  2. 訪問構造對象的原型喜歡這樣:

    var foo = new FooController(); var proto = foo.__proto__; console.log(proto.meh()) // => 'bar'

當你構建foo調用new關鍵字,amoung等,這幾個步驟將occour:

  1. 將創建一個新對象
  2. 該對象的原型w指向FooController的原型;

只要你認爲class methods作爲糖編碼方法,就可以更容易地獲得權力。

希望這會有所幫助。

+0

它不工作,它顯示'[]'在兩種方式(見:https://jsfiddle.net/budiadiono/r2wdgkmk/1/)。我想我會用@Dmitri Pavlutin的答案。 – Adiono

+0

'meh'和'bar'不是可枚舉的屬性。這就是爲什麼'Object.keys()'不起作用。 –

+0

是的,迪米特里是對的。我會編輯我的答案。您可以直接從原型訪問這些方法,但不能使用'keys()'方法列出它們。 –