2017-02-27 51 views
2

是否可以爲所有Javascript對象定義泛型getter/setters?適用於所有Javascript對象的通用Getters/Setters?

我想要做的僞代碼如下。基本上,人和動物路線獲取者設置者CustomGetterCustomSetter

function NewPerson() 
{ 
    var person; 
    var animal; 

    var person.name = 'John Doe'; 
    console.log("Name: " + person.name); //Prints "Name: JOHNDOE CUSTOM" 

    var animal.type = 'Herbivore'; 
    console.log("Animal: " + animal.type); //Prints "Animal: HERBIVORE CUSTOM" 

    console.log("Age: " + person.age); //Prints "Age: NON EXISTANT PROPERTY"; 
} 

function CustomGetter(theObj, propertyName) 
{ 
    if(theObj.hasproperty(propertyName)) 
     return ToUpperCase(theObj.propertyName); 
    else 
    { 
     return "NON EXISTANT PROPERTY"; 
    } 
} 

function CustomSetter(theObj, propertyName, value) 
{ 
    if(theObj.hasproperty(propertyName)) 
     theObj.propertyName = value + " CUSTOM"; 
    else 
    { 
     console.log("NON PROPERTY TO SET"); 
    } 
} 

謝謝!

+0

看一看Object.defineProperty:https://開頭開發er.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty – Psi

+1

對象沒有全面的setter/getter,你需要將你的對象包裝在Proxy中才能獲得這個功能:http ://stackoverflow.com/questions/7891937/is-it-possible-to-implement-dynamic-getters-setters-in-javascript –

回答

1

我只是做了這樣的事情最近。如果您在原型上進行defineProperty,則可以將其應用於所有實例。有點像這樣:

Object.defineProperty(Object.prototype, 'test', { 
     get: function() { 
      return "a test"; 
     } 
    }); 

var test = new Array(2); 
console.log(test); //a test 

現在任何對象都會有'test'屬性。

+1

謝謝。我正在看看它,似乎代理比defineProperty更多的答案。通過代理,我可以捕捉到甚至是無所不包的屬性 – Water

0

你應該使用代理陷阱來做你想做的事。

文檔:MDN - Proxy handlers

你可以輕易地完成與類和原型,但我寧願使用的getter/setter陷阱從一個自定義處理成閉合

通常情況下,它可以看起來像:

var Person = function(profile={}) 
{ 
    var required  = ["age", "name", "profession"]; 
    var buildProfile = function(p) 
    { 
     for(var prop of required) 
     { 
      if(!(prop in p)) 
       throw new ReferenceError 
       ("⛔ Missing required property to profile '" +(prop+" in "+JSON.stringify(profile))+ "'"); 
     } 
     return p; 
    }(profile); 

    var anotherPrivateFunc = function() 
    { 
     // ... 
    }; 

    var publicMethods = 
    { 
     status: function(prop, value) 
     { 
      // ... 
     }, 
     isAdult: function() 
     { 
      return this.age >= 18; 
     }, 
    }; 

    Object.assign(profile, publicMethods); 
    return new Proxy(profile, 
    { 
     get: function(target, prop, receiver) 
     { 
      if(prop in target) 
      { 
       switch(prop) 
       { 
        case "name": 
         return target[prop].toUpperCase(); break; 
       } 
      } 
      return Reflect.get.apply(Object.getOwnPropertyDescriptor, arguments); 
     } 
    }); 
}; 

var person = new Person({age:32, name: "Water", profession:"developper"}) 

person.name  // will return WATER 
person.age  // --> 32 
person.isAdult() // --> true 

var person = new Person({age:32}) // will throw an cusotm error 
Uncaught ReferenceError: ⛔ Missing required property to profile 'name in {"age":3}' 
相關問題