2016-11-24 8 views
0

から地図のプロキシを取得するとき、私は次のコードで立ち往生しています:互換性のない受信機ゲッター

class Infrastructure { 
 
    constructor() { 
 
    this._devices = new Map([ 
 
    \t ['foo', 'bar'] 
 
    ]) 
 
    } 
 
    
 
    get devices() { 
 
    return new Proxy(this._devices, {}) 
 
    } 
 
} 
 

 
const infrastructure = new Infrastructure() 
 
console.log(infrastructure.devices.get('foo'))

次のエラーで失敗します。

Method Map.prototype.get called on incompatible receiver

私はどこかに何かを縛る必要があることを知っていますが、私は一種の迷子であると認めなければなりません。

ありがとうございました!

+0

それは私の* *のProxyクラスではありません@Sreekanth、それは[ES2015プロキシ](https://babeljs.io/docs/learn-es2015/#proxies) – Marvin

+0

'インフラストラクチャです。 devices.get( 'foo') 'この返品は何を期待していますか? – Searching

+0

'bar'を返します。 [地図](https://babeljs.io/docs/learn-es2015/#map-set-weak-map-weak-set)を参照してください。 – Marvin

答えて

0

これは問題を解決します。私は具体的な説明をしたら更新されます。

class Infrastructure { 
 
    constructor() { 
 
    this._devices = new Map([ 
 
     ['foo', 'bar'] 
 
    ]) 
 
    } 
 

 
    get devices() { 
 
    return new Proxy(this._devices, {}) 
 
    } 
 
}; 
 

 
const infrastructure = new Infrastructure() 
 
console.log(infrastructure.devices.get.bind(infrastructure._devices)('foo'))

+0

内部の「プライベート」プロパティーにバインドするという考え方に不快です。おそらく、プロキシを介して自然なAPI(生のMapオブジェクトと同じように)を持つ方法があります。そして、私は 'get()'にアクセスするだけでなく、 'Map'メソッド全体にアクセスしたいと思います。 – Marvin

関連する問題