javascript - JS: json, dynamic method creature and closure -


sorry not clear title,but don't know problem is.

so, need write function creates js objects json , fields begins underscore must create setters , getters.

this test json:

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

this function wrote

function jsontoobject(json){     return json.parse(json,function(key,value){         if (value && typeof value === 'object') {             return (function(value){                 var replacement = {};                 (var k in value) {                     if (object.hasownproperty.call(value, k)) {                         //if 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 public field                         }else{                             replacement[k]=value[k];                         }                     }                 }                 return replacement;             }(value));         }         return value;     }); } 

this how test it:

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 

the problem @ point c - output must 11. output function... (this code after optimization, that's why looks obfucated). mistake?

setter , getter duplicate defined

function jsontoobject(json) {     return json.parse(json, function (key, value) {         if (value && typeof value === 'object') {             return (function (value) {                 var replacement = {};                 (var k in value) {                     if (object.hasownproperty.call(value, k)) {                         //if 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 public field                         } else {                             replacement[k] = value[k];                         }                     }                 }                 return replacement;             }(value));         }         return value;     }); } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -