2016-09-22 27 views
0

對不起,不是很明確的標題,但我不知道問題出在哪裏。JS:json,動態方法生物和關閉

所以,我需要編寫一個函數,它可以從json創建js對象,而對於以下劃線開頭的字段,它必須創建setters和getters。

這是測試JSON:

{ 
"_language":null, 
"_country":null 
} 

這是我寫的

function jsonToObject(json){ 
    return JSON.parse(json,function(key,value){ 
     if (value && typeof value === 'object') { 
      return (function(value){ 
       var replacement = {}; 
       for (var k in value) { 
        if (Object.hasOwnProperty.call(value, k)) { 
         //if this is private field 
         if (k.lastIndexOf("_", 0) === 0){ 
          replacement[k]=value[k]; 
          var name=k.substring(1); 
          var getName="get"+name.charAt(0).toUpperCase() + name.slice(1); 
          replacement.constructor.prototype[getName]=function(){ 
           return this[k]; 
          }; 
          var setName="set"+name.charAt(0).toUpperCase() + name.slice(1); 
          replacement.constructor.prototype[setName]=function(newValue){ 
           this[k]=newValue; 
          }; 
         //if this is public field 
         }else{ 
          replacement[k]=value[k]; 
         } 
        } 
       } 
       return replacement; 
      }(value)); 
     } 
     return value; 
    }); 
} 

這是我如何測試它的功能:

var test1=jsonToObject(data); 
test1.setLanguage("11"); 
console.log("Point A:"+test1.getLanguage());//ouput 11 
var test2=jsonToObject(data); 
test2.setLanguage("22"); 
console.log("Point B:"+test2.getLanguage())//output 22 
console.log("Point C:"+test1.getLanguage());//output function (a){this[c]=a} 
console.log("Point D:"+test2.getLanguage())//output 22 

的問題是,在C點 - 輸出必須是11.然而它的輸出是函數...(這個代碼是優化後的,這就是爲什麼它看起來是obfucated)。我的錯誤在哪裏?

回答

1

setter和getter重複定義

function jsonToObject(json) { 
    return JSON.parse(json, function (key, value) { 
     if (value && typeof value === 'object') { 
      return (function (value) { 
       var replacement = {}; 
       for (var k in value) { 
        if (Object.hasOwnProperty.call(value, k)) { 
         //if this is private field 
         if (k.lastIndexOf("_", 0) === 0) { 
          replacement[k] = value[k]; 
          var name = k.substring(1); 
          var getName = "get" + name.charAt(0).toUpperCase() + name.slice(1); 
          // 
          // check defined 
          // 
          console.log(replacement.constructor.prototype[getName]); 
          // 
          // if defined function, prevent override 
          // 
          if (!/function/.test(typeof replacement.constructor.prototype[getName])) { 
           replacement.constructor.prototype[getName] = function() { 
            return this[k]; 
           }; 
          } 
          var setName = "set" + name.charAt(0).toUpperCase() + name.slice(1); 
          // check defined 
          console.log(replacement.constructor.prototype[setName]); 
          // if defined function, prevent override 
          if (!/function/.test(typeof replacement.constructor.prototype[setName])) { 
           replacement.constructor.prototype[setName] = function (newValue) { 
            this[k] = newValue; 
           }; 
          } 
          //if this is public field 
         } else { 
          replacement[k] = value[k]; 
         } 
        } 
       } 
       return replacement; 
      }(value)); 
     } 
     return value; 
    }); 
} 
+0

謝謝您的建議。但是試一下'console.log(test1);'輸出將會是'Object {_language:null,_country:「11」}'。 –

+0

我解決了這個包裝設置setter和getters在匿名函數中的問題 - >據我瞭解,我們傳遞的值,但沒有引用。 –