是的,直到Proxies到達全力,要實現你想要做的唯一方法是陰影地圖/設置等自己的內置方法。
舉例來說,如果你有你的地圖,像這樣:
var myMap = new Map([ ['key1', 'value1'], ['key2', 'value2']])
你必須有一些包裝將它傳遞到添加內置的方法,例如用於對get/set:
function proxify(obj){
var $fnMapGet = function(key){
console.log('%cmap get', 'color:limegreen', 'key:', key)
if(!Map.prototype.has.call(this, key)){
throw(new Error('No such key: '+ key))
} else {
return Map.prototype.get.call(this, key)
}
}
var $fnMapSet = function(key, value){
console.log('%cmap set', 'color:tomato', 'key:', key, 'value:', value)
if(Map.prototype.has.call(this, key)){
throw(new Error('key is already defined: ' + key))
} else {
if(Map.prototype.get.call(this, key) == value){
console.log('%cmap set', 'color:tomato', '*no change')
return this
}
return Map.prototype.set.call(this, key, value)
}
}
Object.defineProperty(obj, 'get', {
get(){
return $fnMapGet
}
})
Object.defineProperty(obj, 'set', {
get(){
return $fnMapSet
}
})
return obj
}
那麼接下來:
proxify(myMap)
myMap.get('key1') // <= "value1"
myMap.get('key2') // <= "value2"
myMap.get('key3') // <= Uncaught Error: No such key: key3
myMap.set('key3', 'value3') // <= Map {"key1" => "value1", "key2" => "value2", "key3" => "value3"}
myMap.set('key3', 'another value3') // <= Uncaught Error: key is already defined: key3
這將添加到做自己的自定義設置的能力/獲取地圖上,不幾乎和子類Map一樣好,也不像es6代理那麼簡單,但它至少可以工作。
完整的代碼片段如下運行:
var myMap = new Map([ ['key1', 'value1'], ['key2', 'value2']])
function proxify(obj){
\t var $fnMapGet = function(key){
\t \t console.log('get key:', key)
\t \t if(!Map.prototype.has.call(this, key)){
\t \t \t throw(new Error('No such key: '+ key))
\t \t } else {
\t \t \t return Map.prototype.get.call(this, key)
\t \t }
\t }
\t var $fnMapSet = function(key, value){
\t \t console.log('set key:', key, 'value:', value)
\t \t if(Map.prototype.has.call(this, key)){
\t \t \t throw(new Error('key is already defined: ' + key))
\t \t } else {
\t \t \t if(Map.prototype.get.call(this, key) == value){
\t \t \t \t console.log('*no change')
\t \t \t \t return this
\t \t \t }
\t \t \t return Map.prototype.set.call(this, key, value)
\t \t }
\t }
\t Object.defineProperty(obj, 'get', {
\t \t get(){
\t \t \t return $fnMapGet
\t \t }
\t })
\t Object.defineProperty(obj, 'set', {
\t \t get(){
\t \t \t return $fnMapSet
\t \t }
\t })
\t return obj
}
proxify(myMap)
myMap.get('key1')
myMap.get('key2')
try {
myMap.get('key3')
} catch(ex){
console.warn('error:', ex.message)
}
myMap.set('key3', 'value3')
try {
myMap.set('key3', 'another value3')
} catch(ex){
console.warn('error:', ex.message)
}
我剛剛獲得'塊範圍聲明(let,const,函數,類)不支持嚴格模式外' – thefourtheye 2015-04-03 14:53:12
而且我得到'未捕獲的SyntaxError:意外的保留字'。 Chrome可能不支持擴展內置類。 – 2015-04-03 14:57:45
是的,在io.js中,我能夠無誤地編譯它(儘管我必須在頂部使用strict)' – thefourtheye 2015-04-03 14:58:50